Coding - files Flashcards
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?
- Declare the type of 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
- Can be absolute: “/Users/sj4-hunt/Documents/IAP-2018- 2019/sounds/bd.wav”
- Or relative: “text.txt”
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?
- File does not exist
- Incorrect permissions
- Not enough memory
What does the eof() method stand for?
End of file
- returns false if there is still data in the file
- returns true when the file has been read
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
- This allows us to update the value of the variable passed in:
- Variables passed by reference are given the & symbol