Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ File Operations #224

Open
Qingquan-Li opened this issue Nov 17, 2022 · 0 comments
Open

C++ File Operations #224

Qingquan-Li opened this issue Nov 17, 2022 · 0 comments
Labels

Comments

@Qingquan-Li
Copy link
Owner

Qingquan-Li commented Nov 17, 2022

1. File Stream Data Types:

Data Type Description
ifstream Input File Stream. This data type can be used only to read data from files into memory.
ofstream Output File Stream. This data type can be used to create files and write data to them.
fstream File Stream. This data type can be used to create files, write data to them, and read data from them.
  • Input Stream – data stream from which information can be read
    • Ex: cin to read from the keyboard
    • Ex: ifstream objects to read data from files
  • Output Stream – data stream to which information can be written
    • Ex: cout to write to the computer screen
    • Ex: ofstream objects to write data into files

2. File Access (Mode) Flags:

File Access Flag Meaning
ios::app Append mode. If the file already exists, its contents are preserved and all output is written to the end of the file. By default, this flag causes the file to be created if it does nor evist.
ios::ate If the file already exists, its contents are preserved and all output is written to the end of the file.
ios::binary Binary mode. When a file is opened in binary mode, data are written to or read from it in pure binary format. (The default mode is text.)
ios::in Input mode. Data will be read from the file. If the file does not exist, it will not be created, and the open function will fail.
ios::out Output mode. Data will be written to the file. By default, the file's contents will be deleted if it already exists.
ios::trunc If the file already exists, its contents will be deleted (truncated). This is the detault mode used by ios::out.

Opening a File with the File Stream Object Definition Statement:

ifstream inputFile("info.txt");
ofstream outputFile("addresses.txt");

An alternative to using the open member function is to use the file stream object definition statement to open the file:

fstream dataFile("names.txt", ios::in | ios::out);

This statement defines a stream object named dataFile and uses it to open the file names.txt.
The file is opened in both input and output modes. This technique eliminates the need to call the open function when your program knows the name and access mode of the file at the time the object is defined.

Opening Files with Constructors

ifstream, ofstream, and fstream have initialization constructors that take a filename (as a C-string) and an open mode. C++ 11: The filename can be a string object.

  • Stream constructors have overloaded versions that take the same parameters as open
  • These constructors open the file, eliminating the need for a separate call to open
    fstream inFile("myFile.dat", ios::in);

3. Examples

3.1

Write a complete program that reads integers from a file called numbers.txt, and displays their sum on the screen.

If the content of numbers.txt is: 10 20 30 100
The program should print: 160

#include <iostream>
#include <fstream>
using namespace std;

int main() {
	int num;
	int sum = 0;
	// ifstream inputFile;
	fstream inputFile;
	// inputFile.open("numbers.txt");
	inputFile.open("numbers.txt", ios::in);
	while(inputFile >> num)
		sum += num;
	cout << sum;
	
	inputFile.close();
	return 0;
}

3.2

Write a complete program that uses an ifstream object to read integers from a file called numbers.txt, and an ofstream object to write into a file called doubles.txt.

Your program should have a loop that reads five integers from the input file, then writes the double of each (×2) into the output file. Write each value in a separate line.

#include <fstream>

using namespace std;

int main() {
	int num;
	int doubleNum;
	ifstream inputFile;
	ofstream outputFile;
	
	inputFile.open("numbers.txt");
	outputFile.open("doubles.txt");
	
	for (int i = 0; i < 5; i++) {
		// Input (read) data from the file into the variable `num`.
		inputFile >> num;
		doubleNum = num * 2;
		// Output (write) data from the variable `doubleNum` into the file.
		outputFile << doubleNum << endl;
	}
	
	inputFile.close();
	outputFile.close();
	
	return 0;
}

3.3

Write a complete program that uses an ifstream object to read integers from a file called numbers.txt, and an ofstream object to write into a file called evens.txt.

Your program should have a loop that reads each integer from the input file, then writes it into the output file if it is an even number. Write each even number in a separate line.

#include <fstream>
using namespace std;

int main() {
	int num;
	// ifstream object to read data from file.
	ifstream inputFile;
	// ofstream object to write data into file.
	ofstream outputFile;
	inputFile.open("numbers.txt");
	outputFile.open("evens.txt");
	
	// Read data from the file (into the variable `num`).
	while(inputFile >> num) {
		if (num % 2 == 0) {
			// Write data into the file.
			outputFile << num << endl;
		}
	}

        inputFile.close();
	outputFile.close();

	return 0;	
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant