More About File Reading

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

Factorial Preparatory Exercise #include using namespace std; double fact(double); int fact(int); int main(void) { const int n=20; ofstream my_file("results.txt");
C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:
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.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
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.
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.
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.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Arithmetic Operators Operation Operator Example Addition = 9 Subtraction = 1 and = -1 Multiplication * 5 * 4 = 9 Division (integer)
Darbas su failais Arnas Terekas IT 1gr. Vilniaus universitetas Matematikos ir informatikos fakultetas.
String Constructors string str; // creates an empty //string string str(“abc”); // creates a string // from a C-string string str(aString); // creates.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
TEXT FILES. Text Files Files Data saved in external storage and can be referenced by a single name File types Document, image, audio, video, etc. Program.
File I/O ifstreams and ofstreams Sections 11.1 &
File I/O ifstreams and ofstreams. Some istream Operations istream function Description cin >> ch; Extract next non-whitespace character from cin and store.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
File I/O in C++ II. Open() function Open() is a member function in each classes ( fstream, ifstream, ofstream) Void fstream :: open ( const char *filename,
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT11: File I/O CS2311 Computer Programming.
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 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
1. 2 Review How many bits are in a byte? a) 4 b) 8 c) 16 d) 256.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
File Handling in C++.
Lecture 14 Arguments, Classes and Files. Arguments.
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++ 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.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
File Processing.
ifstreams and ofstreams
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CS Computer Science IA: Procedural Programming
CS-103 COMPUTER PROGRAMMING
Functions Manipulating Files
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.
Lab 7: File Input/Output Graham Northup
COMP 2710 Software Construction File I/O
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by
Quiz Next Monday.
Random Number Generation
Basic File I/O and Stream Objects
File Streams and File I/O
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
File Review Declare the File Stream Object Name
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.
CHAPTER 2 Arrays and Vectors.
CHAPTER 4 File Processing.
C++ Programming: chapter 6 – iostream
Reading from and Writing to Files
CHAPTER 2 Arrays and Vectors.
Programming with Data Files
Pointers & Functions.
ifstreams and ofstreams
File I/O in C++ II.
Reading from and Writing to Files
Text Files.
Presentation transcript:

More About File Reading

Prompts: Not Needed input.dat 3 -1 34 56 3 14 12 6 124 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file do cout << “What is your next number? ”; } while (fin >> num); input.dat 3 -1 34 56 3 14 12 6 124

Reading the Entire File #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file fin >> num; while (num != -1) process_data(num); } input.dat 3 11 34 56 3 14 12 6 124 -1

Reading the Entire File #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file fin >> num; while (num != -1) process_data(num); } input.dat 3 11 34 56 3 14 12 6 124 -1

Reading the Entire File #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file fin >> num; while (num != -1) process_data(num); } input.dat 3 11 34 56 3 14 12 6 124 -1 marks end of data

Reading the Entire File #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file // SIZE is size of dataset – known a priori for (int i = 0; i < SIZE; i++) fin >> num; process_data(num); } input.dat 3 -1 34 56 3 14 12 6 124

Reading the Entire File #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file // size of dataset unknown! while (fin >> num) // returns false when last { // data element read process_data(num); } input.dat 3 -1 34 56 3 14 12 6 124

Reading the Entire File #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file while (fin >> num) fin >> num; process_data(num); } input.dat 3 -1 34 56 3 14 12 6 124

Reading the Entire File #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file while (fin >> num) fin >> num; //skips every other datum process_data(num); } input.dat 3 -1 34 56 3 14 12 6 124

Reading the Entire File #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file while (fin >> num) // gets integer data fin >> name; // gets name data process_data(num, name); } input.dat 124663 Price 124645 Hurson 124687 Buechler 124752 Davis

Reading the Entire File #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ... // code for opening the file fin >> num; // necessary for case of // empty file while (!fin.eof()) process_data(num); fin >> num; } input.dat 3 -1 34 56 3 14 12 6 124 Note: eof becomes true only after trying to read past the last datum

Example #include <iostream> #include <fstream> using namespace std; int main() { ifstream in; ofstream out; float next_value; in.open(“input.dat”); out.open(“output.dat”); while (in>>next_value) out<<next_value*next_value<<" "; in.close(); out.close(); return 0; }

Example #include <iostream> #include <fstream> using namespace std; int main() { ifstream in; ofstream out; float next_value; in.open(“input.dat”); out.open(“output.dat”); while (in>>next_value) out<<next_value*next_value<<" "; in.close(); out.close(); return 0; }

Example #include <iostream> #include <fstream> using namespace std; int main() { ifstream in; ofstream out; float next_value; in.open(“input.dat”); out.open(“output.dat”); while (in>>next_value) out<<next_value*next_value<<" "; in.close(); out.close(); return 0; }

Example #include <iostream> #include <fstream> using namespace std; int main() { ifstream in; ofstream out; float next_value; in.open(“input.dat”); out.open(“output.dat”); while (in>>next_value) out<<next_value*next_value<<" "; in.close(); out.close(); return 0; }

Example #include <iostream> #include <fstream> using namespace std; int main() { ifstream in; ofstream out; float next_value; in.open(“input.dat”); out.open(“output.dat”); while (in>>next_value) out<<next_value*next_value<<" "; in.close(); out.close(); return 0; }

Example #include <iostream> #include <fstream> using namespace std; int main() { ifstream in; ofstream out; float next_value; in.open(“input.dat”); out.open(“output.dat”); while (in>>next_value) out<<next_value*next_value<<" "; in.close(); out.close(); return 0; }

Example #include <iostream> #include <fstream> using namespace std; int main() { ifstream in; ofstream out; float next_value; in.open(“input.dat”); out.open(“output.dat”); while (in>>next_value) out<<next_value*next_value<<" "; in.close(); out.close(); return 0; }

Example #include <iostream> #include <fstream> using namespace std; int main() { ifstream in; ofstream out; float next_value; in.open(“input.dat”); out.open(“output.dat”); while (in>>next_value) out<<next_value*next_value<<" "; in.close(); out.close(); return 0; }

Example #include <iostream> #include <fstream> using namespace std; int main() { ifstream in; ofstream out; float next_value; in.open(“input.dat”); out.open(“output.dat”); while (in>>next_value) out<<next_value*next_value<<" "; in.close(); out.close(); return 0; }

End of Session