Reading and writing of files in C++
A file must be opened before you can read from it or write to it. Either the ofstream or fstreamobject may be used to open a file for writing and ifstream object is used to open a file for reading purpose only.
Following is the standard syntax for open() function, which is a member of fstream, ifstream, and ofstream objects.
#d6d6d6;">void open(const char *filename, ios::openmode mode);
Here, the first argument specifies the name and location of the file to be opened and the second argument of the open() member function defines the mode in which the file should be opened.
Mode Flag | Description |
---|---|
ios::app | Append mode. All output to that file to be appended to the end. |
ios::ate | Open a file for output and move the read/write control to the end of the file. |
ios::in | Open a file for reading. |
ios::out | Open a file for writing. |
ios::trunc | If the file already exists, its contents will be truncated before opening the file. |
You can combine two or more of these values by ORing them together. For example if you want to open a file in write mode and want to truncate it in case it already exists, following will be the syntax:
#d6d6d6;">ofstream outfile; outfile.open("file.dat", ios::out | ios::trunc );
Similar way, you can open a file for reading and writing purpose as follows:
#d6d6d6;">fstream afile; afile.open("file.dat", ios::out | ios::in );
When a C++ program terminates it automatically closes flushes all the streams, release all the allocated memory and close all the opened files. But it is always a good practice that a programmer should close all the opened files before program termination.
Following is the standard syntax for close() function, which is a member of fstream, ifstream, and ofstream objects.
#d6d6d6;">void close();
While doing C++ programming, you write information to a file from your program using the stream insertion operator (ofstream or fstream object instead of the cout object.
You read information from a file into your program using the stream extraction operator (>>) just as you use that operator to input information from the keyboard. The only difference is that you use an ifstream or fstream object instead of the cin object.
Following is the C++ program which opens a file in reading and writing mode. After writing information inputted by the user to a file named afile.dat, the program reads information from the file and outputs it onto the screen:
#d6d6d6;">#include #include using namespace std; int main () { char data[100]; // open a file in write mode. ofstream outfile; outfile.open("afile.dat"); cout "Writing to the file" endl; cout "Enter your name: "; cin.getline(data, 100); // write inputted data into the file. outfile data endl; cout "Enter your age: "; cin >> data; cin.ignore(); // again write inputted data into the file. outfile data endl; // close the opened file. outfile.close(); // open a file in read mode. ifstream infile; infile.open("afile.dat"); cout "Reading from the file" endl; infile >> data; // write the data at the screen. cout data endl; // again read the data from the file and display it. infile >> data; cout data endl; // close the opened file. infile.close(); return 0; }
When the above code is compiled and executed, it produces the following sample input and output:
#d6d6d6;">$./a.out Writing to the file Enter your name: Zara Enter your age: 9 Reading from the file Zara 9
Above examples make use of additional functions from cin object, like getline() function to read the line from outside and ignore() function to ignore the extra characters left by previous read statement.
Credits:
http://www.tutorialspoint.com/cplusplus/cpp_files_streams.htm
Continue reading ]]>Reading and writing of files in C++
A file must be opened before you can read from it or write to it. Either the ofstream or fstreamobject may be used to open a file for writing and ifstream object is used to open a file for reading purpose only.
Following is the standard syntax for open() function, which is a member of fstream, ifstream, and ofstream objects.
#d6d6d6;">void open(const char *filename, ios::openmode mode);
Here, the first argument specifies the name and location of the file to be opened and the second argument of the open() member function defines the mode in which the file should be opened.
Mode Flag | Description |
---|---|
ios::app | Append mode. All output to that file to be appended to the end. |
ios::ate | Open a file for output and move the read/write control to the end of the file. |
ios::in | Open a file for reading. |
ios::out | Open a file for writing. |
ios::trunc | If the file already exists, its contents will be truncated before opening the file. |
You can combine two or more of these values by ORing them together. For example if you want to open a file in write mode and want to truncate it in case it already exists, following will be the syntax:
#d6d6d6;">ofstream outfile; outfile.open("file.dat", ios::out | ios::trunc );
Similar way, you can open a file for reading and writing purpose as follows:
#d6d6d6;">fstream afile; afile.open("file.dat", ios::out | ios::in );
When a C++ program terminates it automatically closes flushes all the streams, release all the allocated memory and close all the opened files. But it is always a good practice that a programmer should close all the opened files before program termination.
Following is the standard syntax for close() function, which is a member of fstream, ifstream, and ofstream objects.
#d6d6d6;">void close();
While doing C++ programming, you write information to a file from your program using the stream insertion operator (ofstream or fstream object instead of the cout object.
You read information from a file into your program using the stream extraction operator (>>) just as you use that operator to input information from the keyboard. The only difference is that you use an ifstream or fstream object instead of the cin object.
Following is the C++ program which opens a file in reading and writing mode. After writing information inputted by the user to a file named afile.dat, the program reads information from the file and outputs it onto the screen:
#d6d6d6;">#include#include using namespace std; int main () { char data[100]; // open a file in write mode. ofstream outfile; outfile.open("afile.dat"); cout "Writing to the file" endl; cout "Enter your name: "; cin.getline(data, 100); // write inputted data into the file. outfile data endl; cout "Enter your age: "; cin >> data; cin.ignore(); // again write inputted data into the file. outfile data endl; // close the opened file. outfile.close(); // open a file in read mode. ifstream infile; infile.open("afile.dat"); cout "Reading from the file" endl; infile >> data; // write the data at the screen. cout data endl; // again read the data from the file and display it. infile >> data; cout data endl; // close the opened file. infile.close(); return 0; }
When the above code is compiled and executed, it produces the following sample input and output:
#d6d6d6;">$./a.out Writing to the file Enter your name: Zara Enter your age: 9 Reading from the file Zara 9
Above examples make use of additional functions from cin object, like getline() function to read the line from outside and ignore() function to ignore the extra characters left by previous read statement.
Credits:
http:/
Creation and use of strings in C++
C++ provides following two types of string representations:
The C-style character string.
The string class type introduced with Standard C++.
The standard C++ library provides a string class type that supports all the operations mentioned above, additionally much more functionality. We will study this class in C++ Standard Library but for now let us check following example:
At this point, you may not understand this example because so far we have not discussed Classes and Objects. So can have a look and proceed until you have understanding on Object Oriented Concepts.
#d6d6d6;">#include #include using namespace std; int main () { string str1 = "Hello"; string str2 = "World"; string str3; int len ; // copy str1 into str3 str3 = str1; cout "str3 : " str3 endl; // concatenates str1 and str2 str3 = str1 + str2; cout "str1 + str2 : " str3 endl; // total lenghth of str3 after concatenation len = str3.size(); cout "str3.size() : " len endl; return 0; }
When the above code is compiled and executed, it produces result something as follows:
#d6d6d6;">str3 : Hello str1 + str2 : HelloWorld str3.size() : 10
Credits:
Continue reading ]]>Creation and use of strings in C++
C++ provides following two types of string representations:
The C-style character string.
The string class type introduced with Standard C++.
The standard C++ library provides a string class type that supports all the operations mentioned above, additionally much more functionality. We will study this class in C++ Standard Library but for now let us check following example:
At this point, you may not understand this example because so far we have not discussed Classes and Objects. So can have a look and proceed until you have understanding on Object Oriented Concepts.
#d6d6d6;">#include#include using namespace std; int main () { string str1 = "Hello"; string str2 = "World"; string str3; int len ; // copy str1 into str3 str3 = str1; cout "str3 : " str3 endl; // concatenates str1 and str2 str3 = str1 + str2; cout "str1 + str2 : " str3 endl; // total lenghth of str3 after concatenation len = str3.size(); cout "str3.size() : " len endl; return 0; }
When the above code is compiled and executed, it produces result something as follows:
#d6d6d6;">str3 : Hello str1 + str2 : HelloWorld str3.size() : 10
Credits:
]]>Nesting of conditional statements
It is always legal to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s).
The syntax for a nested if statement is as follows:
#d6d6d6; font-size: 12px; overflow: auto; color: #313131; background-color: #eeeeee;">if( boolean_expression 1) { // Executes when the boolean expression 1 is true if(boolean_expression 2) { // Executes when the boolean expression 2 is true } }
You can nest else if...else in the similar way as you have nested if statement.
#d6d6d6; font-size: 12px; overflow: auto; color: #313131; cursor: default; background-image: url('http://www.tutorialspoint.com/cplusplus/images/try-it.jpg') !important; background-attachment: initial !important; background-color: #eeeeee !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: 100% 0%; background-repeat: no-repeat !important;">#include using namespace std; int main () { // local variable declaration: int a = 100; int b = 200; // check the boolean condition if( a == 100 ) { // if condition is true then check the following if( b == 200 ) { // if condition is true then print the following cout "Value of a is 100 and b is 200" endl; } } cout "Exact value of a is : " a endl; cout "Exact value of b is : " b endl; return 0; }
When the above code is compiled and executed, it produces the following result:
#d6d6d6; font-size: 12px; overflow: auto; color: #313131; background-color: #eeeeee;">Value of a is 100 and b is 200 Exact value of a is : 100 Exact value of b is : 200
Credits:
Continue reading ]]>Nesting of conditional statements
It is always legal to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s).
The syntax for a nested if statement is as follows:
#d6d6d6; font-size: 12px; overflow: auto; color: #313131; background-color: #eeeeee;">if( boolean_expression 1) { // Executes when the boolean expression 1 is true if(boolean_expression 2) { // Executes when the boolean expression 2 is true } }
You can nest else if…else in the similar way as you have nested if statement.
#d6d6d6; font-size: 12px; overflow: auto; color: #313131; cursor: default; background-image: url('http://www.tutorialspoint.com/cplusplus/images/try-it.jpg') !important; background-attachment: initial !important; background-color: #eeeeee !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: 100% 0%; background-repeat: no-repeat !important;">#includeusing namespace std; int main () { // local variable declaration: int a = 100; int b = 200; // check the boolean condition if( a == 100 ) { // if condition is true then check the following if( b == 200 ) { // if condition is true then print the following cout "Value of a is 100 and b is 200" endl; } } cout "Exact value of a is : " a endl; cout "Exact value of b is : " b endl; return 0; }
When the above code is compiled and executed, it produces the following result:
#d6d6d6; font-size: 12px; overflow: auto; color: #313131; background-color: #eeeeee;">Value of a is 100 and b is 200 Exact value of a is : 100 Exact value of b is : 200
Credits:
]]>