Coding - files Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What do fstreams provide?

A
Fstreams provide a series of classes for file input and output. Included in the  library.
#include
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the two classes of file input and output?

A

std: :ofstream file output;
std: :ifstream file input;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you use a file stream?

A
  • Declare the type of stream

- Call open() on the file stream

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Give an example statement of inputting a file stream.

A

fileInput.open (“some.txt”, std::fstream::in);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the first argument within opening a file?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How are folder paths separated?

A

Folder paths are separated with the / symbol

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the second argument within opening a file?

A

The second argument is file options:
- The most commonly used ones are Std::fstream::in
and Std::fstream::out

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Before trying to read or write a file what must we do?

A

We must first check that has open correctly

- Use .is_open()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Give an example of checking if the file works properly.

A
if (fileInput.is_open()) { //opened ok!
}
else {
std::cout << "error opening file \n";
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the reasons for file errors?

A
  • File does not exist
  • Incorrect permissions
  • Not enough memory
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the eof() method stand for?

A

End of file

  • returns false if there is still data in the file
  • returns true when the file has been read
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What do we do once we have finished using a file?

A

We need to call close() on it.

- fileInputStream.close();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the getLine function used for?

A

Reading a single line from the file up to a ‘\n’ character.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the two ways of passing values to a function?

A
By copy:
void copyValue (int number);
By reference:
void referenceValue (int &amp; number);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does a reference mean?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

When should you pass by reference?

A
  • When you want to be able to write to the variable
  • When you want to have multiple return types
  • Pass large data structures that would be expensive to copy
17
Q

What is the order to writing a file?

A
  1. Open file
  2. Check file is open
  3. Write contents
  4. Close file
18
Q

Give an example of the whole file writing process.

A
std::ofstream outputStream
outputStream.open("file.txt", std::ofstream::out);
if (outputStream.is_open()) {
outputStream << 60 << "\n";
}
outputStream.close();
19
Q

How do you write data to a file?

A

outputStream &laquo_space;” “; OR
outputStream &laquo_space;10; //int OR
outputStream &laquo_space;0.01; //float

20
Q

Give an example of the reading process.

A

std: :ifstream myFile(“thisis.txt”);
std: :string text;

while(myFile >> text)
{
    std::cout << text << "\t";
}