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.

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.
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.
#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
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.
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
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.
Arithmetic Operators Operation Operator Example Addition = 9 Subtraction = 1 and = -1 Multiplication * 5 * 4 = 9 Division (integer)
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
String Constructors string str; // creates an empty //string string str(“abc”); // creates a string // from a C-string string str(aString); // creates.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
ITEC 320 C++ Examples.
File I/O ifstreams and ofstreams Sections 11.1 &
Topics 1.File Basics 2.Output Formatting 3.Passing File Stream Objects to Functions 4.More Detailed Error Testing 5.Member Functions for Reading and 6.Writing.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT11: File I/O CS2311 Computer Programming.
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.
Vectors One-Dimensional Containers. Problem A file contains a sequence of names and scores: Ann92 Bob84 Chris89... Using OCD, design and implement a program.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
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.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
ifstreams and ofstreams
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
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.
C++ Arrays.
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.
More About File Reading
Basic File I/O and Stream Objects
File Streams and File I/O
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
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 Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
File I/O.
File I/O in C++ I.
CS150 Introduction to Computer Science 1
CHAPTER 4 File Processing.
Reading from and Writing to Files
Programming with Data Files
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.
Reading from and Writing to Files Part 2
Lecture 9 Files Handling
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

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

Computer Skills2 for Scientific Colleges 2 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. 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)

Computer Skills2 for Scientific Colleges 3 READING DATA FROM FILES USING C++ #include using namespace std; void main() { ifstream indata; // indata is like cin int num; // variable for input value indata.open("example.txt"); indata >> num; while ( !indata.eof() ) { cout << "The next number is " << num << endl; indata >> num; // sets EOF flag if no value found } indata.close(); cout << "End-of-file reached.." << endl; }

Computer Skills2 for Scientific Colleges READING DATA FROM FILES USING C++ The result:

Computer Skills2 for Scientific Colleges 5 READING DATA FROM FILES USING C++ #include using namespace std; void main() { ifstream indata; // indata is like cin char letter; // variable for input value indata.open("example.txt"); // opens the file indata >> letter; while ( letter != 'Q' ) { cout << letter; indata >> letter; } cout << endl; indata.close(); cout << "End-of-file reached.." << endl; }

Computer Skills2 for Scientific Colleges READING DATA FROM FILES USING C++ The result:

Computer Skills2 for Scientific Colleges 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 using namespace std; void main () { ofstream myfile; myfile.open("example.dat"); myfile << "Writing this to a file.\n"; } Computer Skills2 for Scientific Colleges 8

9 WRITING DATA IN THE FILES USING C++

Computer Skills2 for Scientific Colleges Input/Output with files #include using namespace std; void main(){ // To write in a file: ofstream obj_out("file_05.txt"); 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_05.txt"); while(obj_in >> x) cout << "X from the file is: " << x << endl; cout<<endl; }

Computer Skills2 for Scientific Colleges 11 Input/Output with files

Computer Skills2 for Scientific Colleges 12 Example Q1: Write a program that copies a content of a file into a one-dimensional array. Assume that the content of the file is integers.

Computer Skills2 for Scientific Colleges 13 Solution #include 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 for(int i=0; i<c; i++) cout << a[i] << endl; }

14 Example Q2: 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 using namespace std; void main() { string x; int c=0; ifstream fin1("test.txt", ios::in); while(fin1>>x) c++; fin1.close(); ifstream fin2("test.txt", ios::in); string *a = new string[c]; for(int i=0; i<c; i++) fin2 >> a[i]; // To print the content of the array for(int i=0; i<c; i++) cout << a[i] << endl; } Computer Skills2 for Scientific Colleges 15