Lecture 3

C does the necessary translations for you 18 example

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: and fscanf from C then I don’ mind if you use them. t • Files may be text files (files of chars) or binary files. • You still may read and write numbers from and to text files. C++ does the necessary translations for you. 18 Example: Character Input #include <fstream.h> ifstream my_file_var; my_file_var.open(“disk_name”); if (!my_file_var) process_error(); my_file_var >> char_variable; my_file_var.close(); 19 Character Output ofstream another_file_var(“data.dat”); another_file_var << Character; // or another_file_var.put(Character); Useful Functions for Input my_file_var.ignore(2) // skip 2 characters ch = my_file_var.peek(); // return but leave ch = my_file_var.get(); // variant of << syntax while (my_file_var.peek() != EOF) {...} while (my_file_var >> Char) // TRUE until // EOF 20 Numeric I/O from Text Files If the input is numeric, AND if we read into...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online