What do fstreams provide?
Fstreams provide a series of classes for file input and output. Included in the library. #include
What are the two classes of file input and output?
std: :ofstream file output;
std: :ifstream file input;
How do you use a file stream?
- Call open() on the file stream
Give an example statement of inputting a file stream.
fileInput.open (“some.txt”, std::fstream::in);
What is the first argument within opening a file?
The first argument is the file path
How are folder paths separated?
Folder paths are separated with the / symbol
What is the second argument within opening a file?
The second argument is file options:
- The most commonly used ones are Std::fstream::in
and Std::fstream::out
Before trying to read or write a file what must we do?
We must first check that has open correctly
- Use .is_open()
Give an example of checking if the file works properly.
if (fileInput.is_open()) { //opened ok!
}
else {
std::cout << "error opening file \n";
}What are the reasons for file errors?
What does the eof() method stand for?
End of file
What do we do once we have finished using a file?
We need to call close() on it.
- fileInputStream.close();
What is the getLine function used for?
Reading a single line from the file up to a ‘\n’ character.
What are the two ways of passing values to a function?
By copy: void copyValue (int number);
By reference: void referenceValue (int & number);
What does a reference mean?
When we pass by reference we pass the actual variable
When should you pass by reference?
What is the order to writing a file?
Give an example of the whole file writing process.
std::ofstream outputStream
outputStream.open("file.txt", std::ofstream::out);
if (outputStream.is_open()) {
outputStream << 60 << "\n";
}
outputStream.close();How do you write data to a file?
outputStream «_space;” “; OR
outputStream «_space;10; //int OR
outputStream «_space;0.01; //float
Give an example of the reading process.
std: :ifstream myFile(“thisis.txt”);
std: :string text;
while(myFile >> text)
{
std::cout << text << "\t";
}