Definition. Declaration. Opening and closing. Reading. Writing. Exercises. Binary files. Definition. Declaration, opening and closing. Read. Write. Exercises. Errors. Opening and closing (2/2). Shortened version: ifstream fl("ifi.txt"); // by default, ios::in ofstream fe("ofi.txt"); // ios::out. How to check whether the file was opened?
Unit 3
Text files Definition Declaration Opening and closing Reading
Unit 3: C++ Files
Writing Exercises
Programming 2
Binary files Definition Declaration, opening and closing Read Write Exercises
Errors
2013-2014
Index Unit 3
1
Text files Definition Declaration Opening and closing Reading a text file (1/2) Writing a text file Exercises
2
Binary files Definition Declaration, opening and closing Reading a binary file Writing a binary file Exercises
3
Managing read/write errors
Text files Definition Declaration Opening and closing Reading Writing Exercises
Binary files Definition Declaration, opening and closing Read Write Exercises
Errors
What is a text file? Unit 3
Text files Definition Declaration Opening and closing Reading
A text file is also called file with format It only contains printable characters
Writing Exercises
Binary files Definition Declaration, opening and closing Read Write Exercises
Errors
A printable character is a character with an ASCII code greater or equal to 32. What is ASCII code? It’s a code which maps each character to a number (computers only store numbers). Text file examples: C++ source code, a webpage (HTML), a makefile, . . .
Declaration of file variables Unit 3
Text files Definition Declaration Opening and closing Reading Writing Exercises
Binary files Definition Declaration, opening and closing Read Write Exercises
Errors
To use them, add #include : ifstream fich_read; (read only) ofstream fich_write; (write only) fstream fich_read_write; (unusual in text files)
Opening and closing (1/2) Unit 3
Text files Definition Declaration
Opening modes: read, write, read/write, append Files can be opened in C++ using “open”.:
Opening and closing Reading Writing Exercises
const char nombre[]="mifichero.txt"; fichero.open(nombre,ios::in);
Binary files Definition Declaration, opening and closing Read Write Exercises
Errors
When the filename is a string, it must be converted into an array of characters using the function c_str(). C++ opening modes: read write read/write append
ios::in ios::out ios::in | ios::out ios::out | ios::app
(fstream)
Opening and closing (2/2) Unit 3
Shortened version: Text files
ifstream fl("ifi.txt"); // by default, ios::in
Definition Declaration Opening and closing
ofstream fe("ofi.txt"); // ios::out
Reading Writing Exercises
Binary files Definition Declaration, opening and closing
How to check whether the file was opened? How to close it?
Read Write Exercises
Errors
if (fichero.is_open()) { // it can be read now ... fichero.close(); // close the file } else // opening error
Detection of the end of file Unit 3
Using the method “eof”: Text files Definition Declaration Opening and closing
ifstream fi; ... while (!fi.eof() ...)
Reading Writing Exercises
Binary files Definition Declaration, opening and closing Read Write
How does it work? When any data (character, integer, etc.) is asked to be read but there is nothing else in the file, the method returns “true”
Exercises
Errors
Warning: after reading the last valid data, it returns “false’ ’ again, therefore ... It’s necessary to make an extra read (which may return non-valid data that can be ignored) to force the end of file detection.
Reading lines (1/2) Unit 3
Text files Definition Declaration Opening and closing Reading Writing Exercises
Binary files Definition Declaration, opening and closing
... if (fi.is_open()) { getline(fi,s); // ’s’ is string // fi.getline(cad,tCAD); // ’cad’ is char [] while (!fi.eof()) { // do something with ’s’
Read
getline(fi,s);
Write Exercises
}
Errors
fi.close(); }
Reading lines (2/2) Unit 3
What happens if the last line of the file doesn’t end with ‘\n’? then it’s lost and discarded!! Text files Definition Declaration Opening and closing Reading Writing Exercises
Binary files Definition Declaration, opening and closing Read
... if (fi.is_open()) { s=""; getline(fi,s); while (!fi.eof() || s.length()!=0) { // do something with ’s’
Write Exercises
s=""; // initialize ’s’ getline(fi,s);
Errors
} fi.close(); }
Reading single characters Unit 3
Text files Definition Declaration Opening and closing Reading Writing Exercises
Binary files Definition
... if (fi.is_open()) { c = fi.get(); while (!fi.eof()) { // do something with ’c’
Declaration, opening and closing Read
c = fi.get();
Write
}
Exercises
Errors
fi.close(); }
Reading more than one variable Unit 3
Files are actually “streams”, therefore they behave like cin and cout
Text files Definition Declaration Opening and closing Reading Writing
#include ... ifstream fi; int numentero; double numreal;
Exercises
Binary files Definition Declaration, opening and closing Read Write Exercises
Errors
fi.open("mifichero.txt",ios::in); if (fi.is_open()) { fi >> numentero; while (!fi.eof()) { fi >> numreal; // do something with ’numentero’ and ’numreal’ fi >> numentero; // "extra" lecture? } fi.close(); }
Writing a text file Unit 3
Text files Definition Declaration Opening and closing
ofstream fo;
Reading Writing Exercises
Binary files Definition Declaration, opening and closing
fo.open("mifichero.txt",ios::out); if (fo.is_open()) { fo