FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by

Slides:



Advertisements
Similar presentations
C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:
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.
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.
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.
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.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a.
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.
File I/O ifstreams and ofstreams Sections 11.1 &
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
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.
Computer Programming TCP1224 Chapter 13 Sequential File Access.
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.
C++ Programming: chapter 6 – iostream 2014, Spring Pusan National University Ki-Joune Li 1.
Starting Out with C++, 3 rd Edition Basic file operations in C++ Dr. Ahmed Telba.
CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.
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++.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
GE 211 Programming in C ++ Dr. Ahmed Telba Room :Ac -134
C++ Programming: chapter 6 – iostream 2015, Spring Pusan National University Ki-Joune Li 1.
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.
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.
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.
Introduction to Programming
Lecture 9 Files, Pointers
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
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
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.
Quiz Next Monday.
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 Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Control Structures Part 3
Control Structures Part 1
File I/O in C++ I.
C++ Programming: chapter 6 – iostream
Reading from and Writing to Files
Programming with Data Files
Reading Data From and Writing Data To Text Files
ifstreams and ofstreams
Strings Skill Area 313 Part C
File I/O in C++ II.
Lecture 9 Files Handling
Lecture 3 More on Flow Control, More on Functions,
File I/O in C++ I.
Reading from and Writing to Files
Text Files.
Presentation transcript:

FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by Dhimas Ruswanto, BMm.

Lecture Overview Basic File Input Output Output Input

Basic In C++, as well as any programming language, you can read and write to a file. This is done in a similar way to the input/output stream in terms of the library to include with the program. C++ provides 3 of them:

Basic #include <ifstream> Library to specifically deal with input of text files. Does not deal with output. #include <ofstream> Library to specifically deal with output input. #include <fstream> Library to deal with BOTH input and output.

Output To declare the output file stream i.e. for writing we would declare like this: ofstream  theoutputfile; For writing, to connect to a stream, let say opening file testfileio.dat for writing, the file which located in the same folder as the running program.  Previous content of the testfileio.dat will be overwritten, we could write like this: theoutputfile.open("testfileio.dat"); Or with the path: theoutputfile.open("c:\\testfileio.dat "); After we have completed the file processing, we have to close or disconnect the stream to free up the resources to be used by other processes or programs.  Using the close() member function, for output stream: theoutputfile.close();

Output #include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; //declare the variable of type ofstream since you are dealing with output: myfile.open ("example.txt"); //function to open the file which includes the file name: if(myfile.is_open()) //check if the file is open with the is_open() function: myfile << "Hello world! This is output!" << endl; //perform the operation(s): myfile.close(); //function to close the file: }else{ //is_open() returned false and there is a problem: cout << "Can't open the file!" << endl; } return 0;

Output open() The open() function will simply open a file for either input or output. The above program did it for output. The parameter of the function will be the file name. If the file does not exist in the directory, C++ will create it for you. close() A simple function designed to close the file and it's stream. It will require no parameters. is_open() The is_open() function is a boolean function that will check whether or not a file is open. If the function returns true, the file is open without any problems. If it returns false, the file is not good and therefore cannot be used.

Input To declare the input file stream  i.e. for reading we would declare like this: ifstream  theinputfile; Then, to connect to a stream, let say opening file testfileio.dat for reading, the file which located in the same folder as the executable program we would write like this: theinputfile.open("testfileio.dat "); Or with the path we could write: theinputfile.open("c:\\testfileio.dat "); After we have completed the file processing, we have to close or disconnect the stream to free up the resources to be used by other processes or programs.  Using the close() member function, for input stream we would write like this: theinputfile.close();

Input #include <iostream> #include <fstream> #include <string> using namespace std; int main () { string line; ifstream myfile ("example.txt"); //the variable of type ifstream: if (myfile.is_open()) //check to see if the file is opened: { while (! myfile.eof() ) //while there are still lines in the file, keep reading: getline (myfile,line); //place the line from myfile into the line variable: cout << line << endl; //display the line we gathered: } myfile.close(); //close the stream: else cout << "Unable to open file"; return 0; }

Input eof() The eof() function is a boolean function that will check whether or not the file has reached the end. It returns true when the file is at the end and false otherwise.

SUMMARY ! DO and SUBMIT your ASSIGNMENTS