1 / 28

ECE 1305 Introduction to Engineering and Computer Programming

ECE 1305 Introduction to Engineering and Computer Programming. Section 17 File Input and Output. Interactive Data Processing. Prompt User for Input Data. Perform Calculation on Data. More Data?. Batch Data Processing. Read in Data File. Perform Calculation on Data. Write Data to File.

taipa
Download Presentation

ECE 1305 Introduction to Engineering and Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ECE 1305Introduction to Engineering and Computer Programming Section 17File Input and Output

  2. Interactive Data Processing Prompt User for Input Data Perform Calculation on Data More Data?

  3. Batch Data Processing Read in Data File Perform Calculation on Data Write Data to File

  4. Open an Input File #include<fstream> { . . ifstream infile(“c:filename.txt",ios::in); . . . } The header file fstream is require to have access to the file input and output functions. The ifstream command (input file stream) opens a file for input. The variable infile is used to refer to the input file. The data is contained in a text file named filename.txt. If drive c is specified, the compiler looks in the folder that has the *.dsw file. The input-output stream (ios) is set for input (in).

  5. File Input and Output • The simplest file is an ASCII “text” file. • It is simply a sequence of ASCII Characters. • For example the data in this form Hello Dolly $300.00 3.14159 • Would be stored as the sequence of ASCII code characters (in decimal) 072 101 108 108 111 032 068 111 108 108 121 013 010 036 051 048 048 046 048 048 013 010 051 046 049 052 049 053 057 013 010

  6. Read in an Input File #include<fstream> { . . ifstream infile("c:filename.txt",ios::in); int inputVariable; infile >> inputVariable; . . . } The variable inputVariable is declared to receive data from the external file. The stream insert operator >> is used to insert the data from the data file into the program variable a line at a time. To read in multiple lines of data, we need a loop. How do we know when the last line of data has been read?

  7. Example Data File The zero is used to mark the end of the data file.

  8. Read in an Input File The while loop checks to see if the last line of data (zero) has been read, if not, it reads in the next line of data. #include<fstream> { . . const int EOF = 0; ifstream infile("c:filename.txt",ios::in); int inputVariable; infile >> inputVariable; while (inputVariable != EOF) { infile >> inputVariable; } . . }

  9. Read in an Input File This program is not very useful as it is because the value of inputVariable is overwritten each time a new line of data is read in. #include<fstream> { . . const int EOF = 0; ifstream infile("c:filename.txt",ios::in); int inputVariable; infile >> inputVariable; while (inputVariable != EOF) { infile >> inputVariable; } . . }

  10. Open an Output File #include<fstream> { . . ofstream outfile("c:filenameout.txt",ios::out); . . . } The header file fstream is require to have access to the file input and output functions. The ofstream command (output file stream) opens a file for output. The variable outfile is used to refer to the output file. The data is contained in a text file named filenameout.txt on drive c. If the file does not exist it is created. If it exists, it is overwritten. If drive c is specified, the compiler locates the file in the folder that has the *.dsw file. The input-output stream (ios) is set for output (out).

  11. Write to an Output File #include<fstream> { . . ofstream outfile("c:filenameout.txt",ios::out); int outputVariable; outfile << outputVariable<< endl; . . . } The variable outputVariable is declared. The stream insert operator << is used to insert the data from the program variable to the output file a line at a time. An end line is inserted to start a new line. To write multiple lines of data, we need a loop.

  12. Closing Files { . . infile.close(); outfile.close(); . . . } After the file operations are completed, the files must be closed.

  13. Data File

  14. Example Output Screen

  15. Example Output File

  16. File Input Failure #include<fstream> { . . ifstream infile("c:filename.txt",ios::in); int inputVariable; boolfileInputFailure; infile >> inputVariable; fileInputFailure = infile.fail() . . . } If the >> operator finds a valid data type the infile.fail() expression will evaluate to false. If the >> operator finds an invalid data type the infile.fail() expression will evaluate to true.

  17. File Input Failure #include<fstream> { . . ifstream infile("a:filename.txt",ios::in); int inputVariable; boolfileInputFailure; boolendOfData; infile >> inputVariable; fileInputFailure = infile.fail() endOfData = infile.eof() . . . } If the extraction operation has reached the end of the file (EOF), the infile.eof() expression will evaluate to true.

  18. Valid Data File

  19. Output Screen with Valid Data

  20. Output File with Valid Data

  21. Data File with Invalid Data

  22. Output Screen with Invalid Data

  23. Output File with Invalid Data

More Related