when need to keep permanently

Slides:



Advertisements
Similar presentations
Getting Data into Your Program
Advertisements

L which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? l how is a string assigned.
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.
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.
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.
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.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
File input and output in C++ Dr. Ahmed Telba. CSE202: Lecture 9The Ohio State University2 Reading & Writing to Files.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
File I/O ifstreams and ofstreams Sections 11.1 &
Chapter 9 I/O Streams and Data Files
FILE HANDLING IN C++.
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.
I/O in C++ October 7, Junaed Sattar. Stream I/O a stream is a flow of bytes/characters/ints or any type of data input streams: to the program output.
C++ FILE I/O.
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.
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files.
Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.
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.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
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++. 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.
File I/O. Files allow permanent storage of programs and data. ifstream and ofstream objects –For file I/O the inclusion of is required. –Objects for file.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
 Data Streams  Numeric Output  Numeric Input  Multiple Numeric Output  Multiple Numeric Input  Character Output  Character Input  String Output.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
CS212: Object Oriented Analysis and Design
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
Lab 7: File Input/Output Graham Northup
Copyright © 2003 Pearson Education, Inc.
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.
Data Streams.
File I/O.
Standard Input/Output Streams
Standard Input/Output Streams
Lecture 5A File processing Richard Gesick.
File I/O.
File I/O with Records Lesson xx
File Streams and File I/O
Programming with Data Files
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
File I/O.
Reading from and Writing to Files
Programming with Data Files
ifstreams and ofstreams
COMS 261 Computer Science I
Lecture 3 More on Flow Control, More on Functions,
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

when need to keep permanently File Input/Output when need to keep permanently

Streams C++ uses streams for input and output stream - is a sequence of data to be read (input stream) or a sequence of data generated by the program to be output (output stream) by default only standard streams are declared: cin (console input) cout (console output) files are used to store information permanently to do file input/output <fstream> needs to be included as well as using std::ifstream; and using std::ofstream; an input or output stream needs to be declared: ifstream fin; // input stream ofstream fout; // output stream before C++ program can read from or write to a file stream, the stream needs to be connected to a file. This is referred to as opening the file: fin.open(”myfile.dat”); // opening file string declaration and file opening in single statement ifstream fin(”myfile.dat”);

Streams (Cont.) a string variable can be used as file name c_str() function has to be applied to the string: string filename=”myfile.dat”; fin.open(filename.c_str()); after stream variable has been connected to a file, extraction and insertion operators can be used with a stream: fin >> var1 >> var2 >> var3; after the file is opened all the operations on the file is done through associated stream reading from a file while(fin >> myvar){ … } // any file while(getline(fin, myvar)){ … } // text file iterates until file ends or reading error closing stream: fin.close(); // closing file good style: closing all opened file streams before program ends

I/O Error Checking function fail() returns true if previous stream operation failed useful to check if file is opened successfully before performing operations on file useful function exit() - exists program immediately. to use exit, include <cstdlib> exit() accepts one integer as argument: convention: 0 - normal program termination; nonzero - abnormal example fin.open(”myfile.dat”); // opening file if (fin.fail()){ cout << ”Cannot open file myfile.dat”; exit(1); }

Streams as Parameters streams can be passed as parameters streams must be passed by reference void myfunc(ifstream &myinfile);

File I/O Review what is stream? what is the difference between input stream and output stream? what streams do not need definitions? how can you declare a stream? what do the following functions do? open() close() c_str() eof() fail() exit() how does one read from a file until its end? can a stream be passed as a parameter?