Transcript Document

Input/output with files
C++ provides the following classes to perform output and input of characters to/from files:
ofstream: Stream class to write on files
•ifstream: Stream class to read from files
•fstream: Stream class to both read and write from/to files.
Assume that I have a file called Simpletextfile1.txt which looks like this
You can (yes, do) get the file here
Consider the following code
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string line;
/* the following line assumes that you have put the datafile in the same directory as the executable file.
In my case:
c:\users\pkirs\documents\visual studio 2012\Projects\fio1\fio1\
*/
ifstream inputfile ("Simpletextfile1.txt“)
if (inputfile.is_open())
{
while (getline(inputfile,line))
cout << line << endl;
cout << endl;
}
else cout << "unable to open file\n";
return 0;
}
Not surprisingly, this produces the output
We have already used objects whose types were these classes: cin is an object of class istream and cout is an object of class ostream.
Let’s consider the code line-by-line
The Precompiler directives should basically be familiar to you:
#include "stdafx.h“ // we don’t really need file, but it doesn’t hurt
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
We already identified the c++ stream operators :
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files.
We have already used objects whose types were these classes: cin is an object of class istream and
cout is an object of class ostream. (we used iostream because it contains both istream and ostream
I don’t believe we have used the command: string line;
The string declaration is essentially the same as char[] * (i.e., we are dealing with a pointer or an
address (when I checked a blog, I found this suggestion “Always use string: It's easier, it's more
friendly, it's optimized, it's standard, it will prevent you from having bugs, it's been checked and proven
to work.”)
The command: ifstream inputfile ("Simpletextfile1.txt");
Instructs the IDE to open (input file) ("Simpletextfile1.txt“)
Note that we have to check to make sure that the file was found and opened: if (inputfile.is_open())
If not, then there is no sense going any further:
else cout << "unable to open file\n";
return 0;
If it was found and opened, then we read in the text one line at a time (and print it out) and keep reading
[while (getline(inputfile,line))] until we hit an End-of-file (EOF) which is system-dependent (but is
commonly -1, such as in ... as an end-of-file indicator)
Now, please enter in the source code (Slide 2), compile and execute the program
Modify the code so that you write the inputted data to an external file
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string line;
ifstream inputfile ("Simpletextfile1.txt");
// the code below creates a file which will be a copy of the input file
ofstream outputfile (“CopyofSimpletextfile1.txt");
if (inputfile.is_open())
{
while (getline(inputfile,line))
{
cout << line << endl;
outputfile << line << end;
}
cout << endl;
outputfile.close();
}
else cout << "unable to open file\n";
return 0;
}
If you open the files (remember you can find them in project directory: mine is
c:\users\pkirs\documents\visual studio 2012\Projects\fio1\fio1\)
Reading in numeric data
Consider the following data file
This is actually a text file but I would like to read the data into a numeric array:
int matrix[3][3];
r\c
0
1
2
0
1
12
34
24
224
33
98
2
552
8
189
// matrix.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int matrix[3][3];
int rowcount, colcount;
string line, temp;
ifstream inputfile("csvdemo.txt");
if (inputfile.is_open())
{
for (rowcount = 0; rowcount < 3; rowcount++)
{ for (colcount = 0; colcount < 3; colcount++)
{ getline(inputfile, temp, ',');
matrix[rowcount][colcount] = stoi(temp);
cout << "[" << rowcount << "][" << colcount << "] = " << matrix[rowcount][colcount];
}
cout << line << endl;}
inputfile.close();
}
else cout << "unable to open file\n";
return 0;
}
The output would appear as:
The data can also be transferred from an Excel spreadsheet
Save the spreadsheet as csv file:
Save the csv file in your default directory
Change ifstream inputfile("csvdemo.txt"); to ifstream inputfile(“matrixdemo.txt");
Recompile and run
(The output will look exactly the same)
Creating and reading binary files
•
•
•
We know that when we store something in RAM or to a disk, we store the binary equivalents.
That often means converting the data to binary
C/C++ allows you to write and read data in binary formats.
The following from http://courses.cs.vt.edu/cs2604/fall02/binio.html
A file stream object can be opened in one of two ways. First, you can supply a file name along with an i/o mode
parameter to the constructor when declaring an object:
ifstream myFile ("data.bin", ios::in | ios::binary);
Alternatively, after a file stream object has been declared, you can call its open method:
ofstream myFile;
...
myFile.open ("data2.bin", ios::out | ios::binary);