C++ provides the following classes to perform output and input of characters to/from files:...

14
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. Input/output with files Assume that I have a file called Simpletextfile1.txt which looks like this You can (yes, do) get the file here

Transcript of C++ provides the following classes to perform output and input of characters to/from files:...

Page 1: C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:

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.

Input/output with files

Assume that I have a file called Simpletextfile1.txt which looks like this

You can (yes, do) get the file here

Page 2: C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:

#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;}

Consider the following code

Page 3: C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:

Not surprisingly, this produces the output

Page 4: C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:

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 filesifstream: Stream class to read from filesfstream: 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 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

string line;I don’t believe we have used the command:

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.”)

Page 5: C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:

ifstream inputfile ("Simpletextfile1.txt");The command:

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

Page 6: C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:

#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;}

Modify the code so that you write the inputted data to an external file

Page 7: C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:

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\)

Page 8: C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:

Reading in numeric dataConsider 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 12 24 331 34 224 98

2 552 8 189

Page 9: C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:

// 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;}

Page 10: C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:

The output would appear as:

Page 11: C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:

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)

Page 12: C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:

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);

Page 13: C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:
Page 14: C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream: