0% found this document useful (0 votes)
34 views12 pages

C++ Files

Files in C++ allow programs to permanently store and access large amounts of data. To use a file, it must first be opened using ifstream or ofstream objects. Data can then be read from or written to the file by using the filestream objects. Common file operations in C++ include reading/writing individual characters, entire lines of text, or appending new data to the end of the file. Files provide benefits like sharing data between programs and avoiding re-entering information each time a program runs.

Uploaded by

محمد
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views12 pages

C++ Files

Files in C++ allow programs to permanently store and access large amounts of data. To use a file, it must first be opened using ifstream or ofstream objects. Data can then be read from or written to the file by using the filestream objects. Common file operations in C++ include reading/writing individual characters, entire lines of text, or appending new data to the end of the file. Files provide benefits like sharing data between programs and avoiding re-entering information each time a program runs.

Uploaded by

محمد
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Handling File in C++

Using Input/Output Files


A computer file
– is stored on a secondary storage device (e.g., disk);
– is permanent;
– can be used to provide input data to a program or
receive output data from a program, or both;
– must be opened before it is used.
Why to use Files
Convenient way to deal large quantities of data.
Store data permanently (until file is deleted).
Avoid typing data into program multiple times.
Share data between programs.
We need to know:
• how to "connect" file to program
• how to tell the program to read data
• how to tell the program to write data
Files
• To read a file
– We must know its name
– We must open it (for reading)
– Then we can read
– Then we must close it
• That is typically done implicitly
• To write a file
– We must name it
– We must open it (for writing)
• Or create a new file of that name
– Then we can write it
– We must close it
• That is typically done implicitly

4
Using Input/Output Files
• stream - a sequence of characters
– interactive (iostream)
• cin - input stream associated with keyboard.
• cout - output stream associated with display.
– file (fstream)
• ifstream - defines new input stream (normally
associated with a file).
• ofstream - defines new output stream (normally
associated with a file).
Write/Output to a file

File.txt

“This is a test line”


Program This is a test line!

1. Create a filestream object and connect the object with the real file name
ofstream outFile("file1.txt", ios::out);
2. Write to data to the filestream object
outFile << " This is a test line!\ n";

3. Closeto the filestream object


outFile.close();
Output to a file
#include <fstream>
#include <iostream>
using namespace std;

int main(void) Same


{
ofstream outFile("file1.txt", ios::out);
outFile << "That's first line!\n";
outFile.close();
return 0; int main(void)
{
} ofstream outFile;
outFile.open("file1.txt");
outFile << "That's new4!\n";
outFile.close();
return 0;
}
Output to a file (append)
#include <fstream>
#include <iostream>
using namespace std;
This is a test line!

int main(void) This is new line!


{
ofstream outFile("file1.txt", ios::app);
outFile << "This is new line!\n";
outFile.close();
return 0;
}
File I/O Example: Reading file by character
#include <fstream>
#include <iostream>
using namespace std;
int main(void)
{
ifstream openFile("file1.txt"); //open a text file file2.txt
char ch;
while(!openFile.eof())
{
openFile.get(ch);
cout << ch;
} This is a test line!
openFile.close();
return 0;
}
File I/O Example: Reading by line
#include <iostream>
#include <fstream>
using namespace std;
sample.txt
int main()
{ ifstream fin;
string line; This is line1!
fin.open("file1.txt"); This is line2!
// Execute a loop until EOF (End of File)
while (fin) {
// Read a Line from File
getline(fin, line);
// Print line in Console
cout << line << endl;
}
// Close the file
fin.close();
return 0;
}
File I/O Example: Writing by line
#include <iostream>
#include <fstream>
using namespace std;
int main()
{ ofstream fout; // Creation of ofstream class object
string line;
fout.open("file1.txt");
// Execute a loop If file successfully opened
while (fout) {
getline(cin, line); // // Read a Line from standard input
// Press -1 to exit
if (line == "-1")
break;
fout << line << endl; // Write line in file
} // Close the File
fout.close();
return 0; }

You might also like