Lecture 9 Files Handling

Slides:



Advertisements
Similar presentations
Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.
Advertisements

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 Text File I/O Chapter 6 Pages File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
CPS120: Introduction to Computer Science Lecture 15 B 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.
Copyright © 2012 Pearson Education, Inc. Chapter 12: Advanced File Operations.
Chapter 5: Loops and Files.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Chapter 3 Interactivity and Expressions CSC 125 Introduction to C++ programming.
Darbas su failais Arnas Terekas IT 1gr. Vilniaus universitetas Matematikos ir informatikos fakultetas.
Chapter 8 Data File Basics.
C++ Introduction : C++ compilation. Visual Studio 2008 : Creating Command-Line Program.
Chapter 9 I/O Streams and Data Files
1 CS161 Introduction to Computer Science Topic #13.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 12: Advanced File Operations.
Programming Principles II Lecture Notes 7 Files Andreas Savva.
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.
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.
Loops and Files. 5.1 The Increment and Decrement Operators.
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++.
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.
C++ Programming: chapter 6 – iostream 2015, Spring Pusan National University Ki-Joune Li 1.
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.
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.
Ms N Nashandi Dr SH Nggada 2016/01/03Ms N Nashandi and Dr SH Nggada1 Week 6 -File input and output in C++
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.
CS212: Object Oriented Analysis and Design
Chapter 14: Sequential Access Files
Lecture 3 Expressions, Type Conversion, Math and String
What is wrong in the following function definition
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Basic file operations in C++
Introduction to Programming
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.
Basic Input and Output Operations
Copyright © 2003 Pearson Education, Inc.
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
Chapter 5: Looping Starting Out with C++ Early Objects Eighth Edition
Random Number Generation
Interactive I/O Input from keyboard Must prompt user User friendly
Chapter 9 File Streams Computing Fundamentals with C++ 3rd Edition
Chapter 12: Advanced File Operations.
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CS150 Introduction to Computer Science 1
File I/O in C++ I.
Formatted Input, Output & File Input, Output
CS150 Introduction to Computer Science 1
CPS120: Introduction to Computer Science
CHAPTER 4 File Processing.
C++ Programming: chapter 6 – iostream
Reading from and Writing to Files
File I/O in C++ II.
File I/O in C++ I.
Reading from and Writing to Files
Text Files.
Presentation transcript:

Lecture 9 Files Handling CS 128 Introduction to C++ Lecture 9 Files Handling Sampath Jayarathna Cal Poly Pomona Based on slides created by Bjarne Stroustrup & Tony Gaddis

Using Files for Data Storage Can use files instead of keyboard, monitor screen for program input, output Allows data to be retained between program runs Steps: Open the file Use the file (read from, write to, or both) Close the file

File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to files Used in many applications: Word processing Databases Spreadsheets Compilers

Files: What is Needed Use fstream header file for file access File stream types: 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. Define file stream objects: ifstream infile; ofstream outfile; Can use >> to read from, and << write to a file Can use eof member function to test for end of input file

Writing to a new file #include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; myfile.open("example.dat"); // or .txt myfile << "Writing this to a file.\n"; myfile.close(); return 0; }

Testing for File Open Errors Can test a file stream object to detect if an open operation failed: ifstream infile; infile.open("test.txt"); if (!infile) { cout << "File open failure!"; } Can also use the fail member function

Using Files Writing with is_open() method // writing on a text file #include <iostream> #include <fstream> using namespace std; void main () { ofstream myfile ("example.txt"); if (myfile.is_open()) { myfile << "This is a line.\n"; myfile << "This is another line.\n"; myfile.close(); } else cout << "Unable to open file";

Activity 23 Create a program to write information to an external file called “bio.txt” There should a writeToFile() that takes information to write to a file as parameters. Use getUserInput() to create data for 3 users taken from user input. The format should be exactly **** Profile **** Name: <your name> Age: <DOB> City: <city>

Using Files reading with is_open() method // Reading a text file string line; ifstream myfile("example.txt"); if (myfile.is_open()) { while( getline(myfile,line) ) { cout << line << '\n'; } myfile.close(); else cout << "Unable to open file";

Closing Files Use the close member function: infile.close(); outfile.close(); Don’t wait for operating system to close files at program end: may be limit on number of open files may be buffered output data waiting to send to file

Activity 24 Create a program that generate 500 random numbers and write them to an external file. Your function name should be generateRandoms() Use int val = rand() % 100 + 1; to generate a number between 1-100. Include <ctime> and srand(time(NULL)); before your rand() call to initialize the random seed values. Read the files in another function called getRandoms() and display the results.