Software Development -8
C++ Files
File structure; when we want to keep data, it is written as 1 and 0 in the operating system, we need a file structure so that this data can be received and kept as a whole or block, that is, information as a whole. In this way, the data of the drawer’s file can be distinguished from the data of the readers’ file (secondary memory) in secondary memory.
File processing; we can create a file, open a file, read a file, and close a file with the codes we write. Let’s look at the details together;
· ifstream = “input file stream” which is a type of istream or “input stream”
· ofstream = “output file stream” which is a type of ostream or “output stream”
· fstream = “(bidirectional) file stream” as in iostream (“input/output stream”)
File Creation
File Creation
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ofstream fileCreate(“wecreateafile.txt”); // can be like txt.xls.doc
fileCreate.close();
return 0;
}
First, we defined the library #include <fstream> in which we will use ifstream, ofstream, and fstream classes.
Ofstream : file writing
Ifstream : file reading
Fstream: file writing and reading
Let’s look at it step by step by making an application;
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
Get Ceren Ağım’s stories in your inbox
Join Medium for free to get updates from this writer.
ofstream fileWrite(“names.txt”);
fileWrite.close();
return 0;
}
It created an empty .txt file called Names and left it. There is a second way to open the file, it can also be opened with this way to open the file, I have added its code below. The feature of this is that we use “open”, you can use whichever is easier, if you see it in other codes, I have added it so that you are not surprised.
Then let’s add a name to this names.txt file created; Here it is the same as cout<< logic.
Dosyaya baktımızdada aşağıdaki gibi yazılmış olur. Close() kullanıyoruz mutlaka, bunun nedenni hafızada gereksiz yer kaplamaması için.
Ifstream
How to read data from a file, we will do it with ifstream.
When we want to read the names text file, we need to know how many lines and columns there are in the text file so that we can see how it will print it on the screen. Here we are getting data from a file, we need a variable to write the code and process it.
Instead of getline(cin, we set a variable called line so that it can save the name of our file in a variable.
Then we read the first line of the names file from the variable line(string line): Jane, Alex, Henry.
A lot of data could have been added here, line by line, column by column, we will combine it with a while loop so that we can check if we have reached the end each time. This loop will make it easy for us to say that when it finishes reading the rows, it will no longer return, it will stop.
Also if we want to retrieve the contents of the file word by word,
The name of the file >> word; with (like getline and cin >> logic, read from the file and write what you read into the word).
Cpp-file-modes:
Let’s make an application;
//Let this application be a restaurant application, let’s create a menu that can be selected here and add products, let the added products be saved in a .txt file, then list the products and find a product (that is, read the urunler.txt file created with ifstream), for example, category: soups, appetizers, etc. //
It was expected:
Opened a txt file named “product” and added a product/item to it;
Love youuu, work work hard 😊










