File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.

Slides:



Advertisements
Similar presentations
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
Advertisements

1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
Computer Skills2 for Scientific Colleges 1 File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
File I/O. COMP104 File I/O / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
File Handling Accessing a data file for input: 1) Include the file stream header file: #include using namespace std; 2) Associate an input file name with.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Darbas su failais Arnas Terekas IT 1gr. Vilniaus universitetas Matematikos ir informatikos fakultetas.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
File I/O ifstreams and ofstreams Sections 11.1 &
C++ Introduction : C++ compilation. Visual Studio 2008 : Creating Command-Line Program.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
FILE I/O IN C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
Starting Out with C++, 3 rd Edition Basic file operations in C++ Dr. Ahmed Telba.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
Lecture 14 Arguments, Classes and Files. Arguments.
Files To read a file – We must know its name – We must open it (for reading) – Then we can read – Then we must close it That is typically done implicitly.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
Basic file operations in C++ Dr. Ahmed Telba 1. 2 Learning Objectives §C++ I/O streams. §Reading and writing sequential files. §Reading and writing random.
Computer Programming II Lecture 9. Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
Chapter 14: Sequential Access Files
What is wrong in the following function definition
ifstreams and ofstreams
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Basic file operations in C++
Lecture 9 Files, Pointers
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? how is.
FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by
Data Streams.
File I/O with Records Lesson xx
Basic File I/O and Stream Objects
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included for string manipulation? how is.
Standard Input/Output Stream
File I/O.
CS150 Introduction to Computer Science 1
File I/O in C++ I.
CS150 Introduction to Computer Science 1
CHAPTER 4 File Processing.
C++ Programming: chapter 6 – iostream
Reading from and Writing to Files
Programming with Data Files
C++ Programming Lecture 8 File Processing
ifstreams and ofstreams
File Handling Accessing a data file for input: 1) Include the file stream header file: #include using namespace std; 2) Associate an input file.
File I/O in C++ II.
Reading from and Writing to Files Part 2
Lecture 9 Files Handling
File I/O in C++ I.
Reading from and Writing to Files
Input/Output Streams, Part 2
Presentation transcript:

File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed

File Processing in C++ 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.

Open a file Each one of the open() member functions of the classes ofstream, ifstream and fstream has a default mode that is used if the file is opened without a second argument: class default mode parameter ofstream ios::out ifstream ios::in fstream ios::in | ios::out

Reading data from files using C++ The commands for reading data from a file are (for the most part) very similar to those used to read data from the keyboard.

Reading data from files using C++ In order for your program to read from the file, you must: include the fstream header file declare a variable of type ifstream open the file read from the file after each read, check for end-of-file using the eof() member function close the file when access is no longer needed (optional, but a good practice)

Example Q1: Write a program that reads integer numbers from the following file and print the result in output screen.

Reading data from files using C++ #include<iostream> #include <fstream> using namespace std; int main() { ifstream indata; // indata is like cin int num; // variable for input value indata.open(“example.txt",ios::in); indata >> num; while ( !indata.eof() ) { cout << "The next number is " << num << endl; } indata.close(); cout << "End-of-file reached.." << endl;

Reading data from files using C++ The result:

Example Q2: Write a program that reads characters from the following file as long as the character not Q. and print the result in output screen.

Reading data from files using C++ #include<iostream> #include <fstream> using namespace std; void main() { ifstream indata; // indata is like cin char letter; // variable for input value indata.open("example.txt“,ios::in); // opens the file indata >> letter; while ( letter != 'Q' ) { cout << letter; } cout << endl; indata.close(); cout << "End-of-file reached.." << endl;

Reading data from files using C++ The result:

Writing data in the files using C++ In order for your program to writein the file, you must: include the fstream header file declare a variable of type ofstream open the file Writ in the file close the file when access is no longer needed (optional, but a good practice)

Writing data in the files using C++ #include <iostream> #include <fstream> using namespace std; void main () { ofstream myfile; myfile.open("test1.txt",ios::out); myfile << "Writing this to a file......\n"; myfile.close(); }

Writing data in the files using C++

Example Q3: Write a program that read 4integer numbers from the user and write them in a file .then read the numbers from a file and print them in output screen.

Input/Output with files #include <iostream> #include <fstream> using namespace std; void main(){ ofstream obj_out("file_05.txt",ios::out); int x; for(int i=0;i<=3;i++) { cin >> x; obj_out << x << endl; } ifstream obj_in("file_05.txt",ios::in); obj_in >> x; while(!obj_in.eof()) cout << "X from the file is: " << x << endl;

Input/Output with files #include <iostream> #include <fstream> using namespace std; void main(){ // To write in a file: ofstream obj_out("file_02.txt",ios::out); int x; while(cin >> x) obj_out << x << endl; // To stop writing to a file, press CTRL+Z then ENTER // To read from a file: ifstream obj_in("file_02.txt",ios::in); while(obj_in >> x) cout << "X from the file is: " << x << endl; }

Input/Output with files

Example Q: Write a program that copies a content of a file into a one-dimensional array. Assume that the content of the file is integers.

Solution #include<iostream> #include<fstream> using namespace std; void main() { int x, c=0; ifstream fin1("d:\\test.txt", ios::in); while(fin1>>x) c++; ifstream fin2("d:\\test.txt", ios::in); int *a = new int[c]; for(int i=0; i<c; i++) fin2 >> a[i]; // To print the content of the array cout << a[i] << endl; }

We can use the same variable from type fstream for input and output in the same time as the following example.

Input/Output with files #include <iostream> #include <fstream> using namespace std; void main(){ fstream obj("file_05.txt"); int x; for(int i=0;i<=3;i++) { cin >> x; obj << x << endl; } obj.close(); obj.open("file_05.txt"); obj>> x; while(!obj.eof()) cout << "X from the file is: " << x << endl;

Example Q: Write a program that copies a content of a file into a one-dimensional array. Assume that the content of the file is string.

Solution #include<iostream> #include<fstream> #include<string> using namespace std; void main() { string x; int c=0; ifstream fin1("test.txt"); while(fin1>>x) c++; fin1.close(); ifstream fin2("test.txt“); //note: we can use the same variable fin1.open("test.txt“); string *a = new string[c]; for(int i=0; i<c; i++) fin2 >> a[i]; // To print the content of the array cout << a[i] << endl; }