st11-file-sol

st11-file-sol - CSC 1111 Introduction to Computing using C...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CSC 1111 Introduction to Computing using C++ File I/O (Self-test questions with solutions)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 What's the output? ofstream fout("test.dat"); fout << "Hello"; fout.close(); fout.open("test.dat"); fout.close(); What will the file "test.dat" contain?
Background image of page 2
3 Nothing. When a file is opened for output, its content is immediately "erased" (the file size is truncated to 0).
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 What's the output? char ch; ifstream fin("input.txt"); while (!fin.eof()) { fin >> ch; cout << ch << endl; } fin.close(); What's the output? 1 23 4 A 5 Content of file "input.txt"
Background image of page 4
5 White space characters are skipped when reading characters using >>. EOF is encountered only when attempting to read another character after the last character has been read. To avoid this from happening, you can check if every input operation is successful before processing the input, as in: while (!fin.eof()) { if (fin >> ch) cout << ch << endl; } 1 2 3 4 A 5 5 Output
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
6 What's the output? char ch = 'X';
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 18

st11-file-sol - CSC 1111 Introduction to Computing using C...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online