Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:"— Presentation transcript:

1 C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream: Stream class to read from filesifstream fstream: Stream class to both read and write from/to files.fstream 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

2 #include "stdafx.h" #include 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

3 Not surprisingly, this produces the output

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

5 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

6 #include "stdafx.h" #include 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

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

8 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

9 // matrix.cpp : Defines the entry point for the console application. #include "stdafx.h" #include 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; }

10 The output would appear as:

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

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

13

14


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

Similar presentations


Ads by Google