/ Published in: C++
Expand |
Embed | Plain Text
#include <iostream> #include <fstream> //this includes the necessary functions for file IO #include <string> using namespace std; /* Here is another important topic. File input-output. In this program we are going to see how to open a text file, write something and then read this file. The file IO operations are done via streams, similarly to the way we did it for input and output in the console. However I must note that this is the case when writing files and data in text format. We are going to see binary files later. As we said we use streams. Therefore we use the '<<' and '>>' operators. The main logic is this: 1)We create a stream suitable for an input or output file 2)We open a file and link it to the stream we created. This file must be either for input or output and it must agree with the stream type 3)We use the '<<' or '>>' operators to write or read in/from the stream/file 4)We close the stream/file/ See the code below to get how this is done. What we are doing in this program is that we ask the user whether he wants to create a new file with his name and age inside or see what has already been stored in the file. According to the user's choice we either open a new file to write in(if this file exists it is overwritten) and write his name and age or open an existing file (if it exists) and read his name and age. */ int main() { string name;//Variables that hold the users name and age double age; int choice;//A control variable for the user's choice //We ask the user what he wants to do cout<<"What do you want to do?n"; cout<<"(1) Write a file with your name and agen"; cout<<"(2) Read from a file your name and agen"; cin >> choice; if (choice==1){//The user wants a new file cout<<"Please enter your name:n";//we ask the user for this name and age cin>>name; cout<<"Please enter your age:n"; cin>>age; //We create a new stream to an output file. Note the type is 'ofstream' meaning //a stream to a file for output. The name of this variable is the name of the used //stream and for us it holds the name to the file (but not the filename) ofstream output_file_stream; /* Here we open a new file named 'file1.txt' using the 'open()' function of the stream. Note that we enclose this function in an 'if' statement. We do that because if the 'open()' function fails to open the file then it returns a 'false' otherwise if all goes well it returns a 'true'. So we use this if to inform the user as to how the opening of the file went. Alternatively we can use the 'is_open()' function which returns true if a file is open and false if is not. the syntax is 'file_stream.is_open()' Note the syntax of the open statement: file_stream_name.open(filename,input_or_output_identifier) filename: the fact that we are going to write text in the file doesnot mean that the extension must be txt. It could be .dat or nothing at all or anything input_or_output_identifier: this lets the compiler know how we are going to use this file. We want to write in the file so we use ios::out. These identifiers are members of the 'ios' namespace and therefore we user the '::' operator. There are many identifiers but we use here only 'ios::out' which means we open a file to write in (output file) and 'ios::in' which means we open a file to read from (input file) */ if (output_file_stream.open("file1.txt"),ios::out) { /*Note that we use the stream to the file with the '<<' operator to write * anything we want inside just like we used the 'cout' stream for the console * * IMPORTANT: note the usage of 'n' between the variables. This is neccessary to distinguish * the different variables. BEWARE: The usage of 'endl' instead of 'n' does NOT work */ output_file_stream<<name<<"n"<<age; //output_file_stream<<age; } else cout<<"The file could not be openedn"; //We close the file to complete the IO operation output_file_stream.close(); } else if (choice==2){ ifstream input_file_stream; /* Here the only difference from above is that we open a file that should already exist. If it doen't then the 'open' function returns a false and the program stops with an error. If it does then we read the data with the same order as when we wrote it and we place that data in their variables.*/ if (input_file_stream.open("file1.txt"),ios::in){ input_file_stream>>name; input_file_stream>>age; //We print the data from the file cout<<"Your name is '"<<name<<"' and you age is "<<age<<endl; //We close the file input_file_stream.close(); } else cout<<"The file could not be openedn"; } return 0; }
You need to login to post a comment.
