c_-_14_-_file_IO_-_exception_handling

c_-_14_-_file_IO_-_exception_handling - Chapter 12 File...

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

View Full Document Right Arrow Icon
Chapter 12 File Input and Output
Background image of page 1

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

View Full DocumentRight Arrow Icon
Overview Ifstream class For reading data from a file. ofstream class For writing data to a file. fstream class For updating data in a file. #include <fstream>
Background image of page 2
#include <iostream> #include <fstream> using namespace std; void main() { ofstream output; output.open("scores.txt"); output << "John" << " " << "Smith" << " " << 90 << endl; output << "Eric" << " " << "Jones" << " " << 85; output.close(); cout << "done" << endl; } Text I/O – Writing Data to A File Open a file Write data to the file Close the file.If this function is not invoked, the data may not be saved properly in the file. Declare a object of ofstream class <fstream> must be included
Background image of page 3

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

View Full DocumentRight Arrow Icon
#include <iostream> #include <fstream> using namespace std; void main() { ofstream output; output.open("scores.txt"); output << "John" << " " << "Smith" << " " << 90 << endl; output << "Eric" << " " << "Jones" << " " << 85; output.close(); cout << "done" << endl; } Text I/O – Writing Data to A File What would happen if this file already exists? If a file already exists, the contents of the file will be destroyed without warning .
Background image of page 4
#include <iostream> #include <fstream> using namespace std; void main() { ofstream output; output.open("scores.txt"); output << "John" << " " << "Smith" << " " << 90 << endl; output << "Eric" << " " << "Jones" << " " << 85; output.close(); cout << "done" << endl; } Text I/O – Writing Data to A File Where will this file be saved on the hard disk?
Background image of page 5

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

View Full DocumentRight Arrow Icon
Text I/O – Writing Data to A File How should I do if I want to write the data in the file: c:\example\scores.txt output.open(" c:\\example\\scores.txt ");
Background image of page 6
Relative File name Absolute file name is platform dependent. It is better to use relative file name without drive letters. ..\ means the parent directory of current directory output.open(". .\\sample.txt", "r") ; .\ means the current directory output.open(".\\sample.txt","r"); output.open("sample.txt","r") ;
Background image of page 7

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

View Full DocumentRight Arrow Icon
#include <iostream> #include <fstream> using namespace std; void main() { ifstream input; input.open("score.txt"); char firstName[80]; char lastName[80]; int score; input >> firstName >> lastName >> score; cout << firstName << " " << lastName << " " << score << endl; input >> firstName >> lastName >> score; cout << firstName << " " << lastName << " " << score << endl; input.close(); } Text I/O – Reading Data From a File Open a file Read data from the file Close the file Declare a object of ifstream class <fstream> must be included
Background image of page 8
#include <iostream> #include <fstream> using namespace std; void main() { ifstream input; input.open("score.txt"); char firstName[80]; char lastName[80]; int score; input >> firstName >> lastName >> score; cout << firstName << " " << lastName << " " << score << endl; input >> firstName >> lastName >> score; cout << firstName << " " << lastName << " " << score << endl; input.close(); } Text I/O – Reading Data From a File How to check whether a file exists?
Background image of page 9

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

View Full DocumentRight Arrow Icon
void main() { ifstream input; input.open("score.txt"); if (input.fail()) { cout << "File does not exist!" << endl; } else { // Do reading here } } Testing File Existence
Background image of page 10
Testing End of File How do you know how many lines are there in the file?
Background image of page 11

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

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 73

c_-_14_-_file_IO_-_exception_handling - Chapter 12 File...

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

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