STARTING OUT WITH STARTING OUT WITH Class 16 Honors.

Slides:



Advertisements
Similar presentations
Starting Out with C++, 3 rd Edition 1 Chapter 12 – File Operations.
Advertisements

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.
STARTING OUT WITH STARTING OUT WITH Class 15 Honors.
File I/O in C++. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk);is stored on a secondary storage device.
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.
CPSC 231 D.H. C++ File Processing 1 Learning Objectives §C++ I/O streams. §Reading and writing sequential files. §Reading and writing random access files.
計算機概論實習 Files and Streams C++ views file as sequence of bytes Ends with end-of-file marker n-1 end-of-file marker 67 This is.
CS-212 C++ I/O Dick Steflik. C++ I/O Modeled after UNIX’s concept of a “stream” –conceptionally a stream is a continuous flow of characters/bytes from.
Object Oriented Code for a Relative File. Design Questions Private Data the I/O file Record Count Private Methods Conversion from RRN to File Address.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
1 File I/O In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
1 Techniques of Programming CSCI 131 Lecture 29 Search.
Programming Initializing Data of Struct Type. COMP102 Prog. Fundamentals: initialize struct type/ Slide 2 Ex. 10: Initialize Data of struct Type l By.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
File handling in C++ BCA Sem III K.I.R.A.S. Using Input/Output Files Files in C++ are interpreted as a sequence of bytes stored on some storage media.
Starting Out with C++, 3 rd Edition Basic file operations in C++ Dr. Ahmed Telba.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Stack/Queue - File Processing Lecture 10 March 29, 2005.
Starting Out with C++, 3 rd Edition 1 File Operations in C++
File I/O. fstream files File: similar to vector of elements Used for input and output Elements of file can be –character (text)struct –object (non-text.
C++ FILE I/O.
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,
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.
Starting Out with C++, 3 rd Edition Basic file operations in C++ Dr. Ahmed Telba.
C++ Programming: chapter 6 – iostream 2014, Spring Pusan National University Ki-Joune Li 1.
Arrays in C++: Numeric Character (Part 2). Passing Arrays as Arguments in C++, arrays are always passed by reference (Pointer) whenever an array is passed.
DCT1063 Programming 2 CHAPTER 3 FILE INPUT AND FILE OUTPUT Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Member Functions for Reading and Writing Files 12.5.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Starting Out with C++, 3 rd Edition Basic file operations in C++ Dr. Ahmed Telba.
Starting Out with C++, 3 rd Edition 1 Chapter 12 – File Operations.
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!"
File Handling. Read data from file and display it on screen #include int main() { /* fin object is created with parameterzied constructor */ ifstream.
1 CSC241: Object Oriented Programming Lecture No 32.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
C++ Programming: chapter 6 – iostream 2015, Spring Pusan National University Ki-Joune Li 1.
CSCI 333 Data Structures I/O with C++ 20 October, 2003.
Lecture 14 Arguments, Classes and Files. Arguments.
Binary Files. Text Files vs. Binary Files Text files: A way to store data in a secondary storage device using Text representation (e.g., ASCII characters)
 Working with multiple files  Copying files  Updating files.
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.
1 Huffman Codes Using Binary Files. 2 Getting Started Last class we extended a program to create a Huffman code and permit the user to encode and decode.
Starting Out with C++ 1 Chapter 12: File Operations What is a File? A file is a collection on information, usually stored on some electronic medium. Information.
KBD Program with file I/O Operation File Disk Program with file I/O Operation Screen Cin Cout.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
2008YeungNam Univ. SE Lab. 1 C++ views each file as a sequence of bytes terminated by EOF-marker. Header files Files are opened by creating objects of.
What is wrong in the following function definition
MT262A Review.
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
File Handling Operations in C++
CPSC 231 D.H. C++ File Processing
Dynamic Memory Allocation Reference Variables
File I/O with Records Lesson xx
آشنایی با ساختارها و کار با فایلها
Today’s Lecture I/O Streams Tools for File I/O
Chapter 13: Advanced File and I/O Operations
Sequential input and output Operations in file
C++ FileIO pepper.
Topics Input and Output Streams More Detailed Error Testing
Chapter 12: Advanced File Operations.
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CHAPTER 4 File Processing.
C++ Programming: chapter 6 – iostream
C++ Programming Lecture 8 File Processing
File I/O in C++ II.
Presentation transcript:

STARTING OUT WITH STARTING OUT WITH Class 16 Honors

2 Create records with structures Divide typical file processing algorithms into functions Objectives

3 Keyboard - enter data

4 memory

5 Keyboard - enter data memory

6 Creating Records with Structures #include #include // for toupper // Modified from textbook //Declare a structure for a record. struct Info { char Name[15]; int age; }; P

7 Creating Records with Structures int main() { fstream Outfile(“people.dat”, ios::out | ios::binary); Info P; char Again; if (Outfile ==0) {cout << “Error opening file. Program aborting.\n; return; } GetData(P,Outfile); } P

8 void GetData(Info &Person,fstream &People) {do { char buffer[80]; cout << “Enter the persons name; cin.getline(Person.Name, 15); do { flag =0; cout << “Age: ”; cin.getline(buffer, 4); for (int index=0; index<strlen(buffer); index++) if (!isdigit(buffer[index])) { flag=1; cout << “invalid data entered”; break; } } while (flag == 1); Person.age=atoi(buffer); People.write((char *)&Person, sizeof (Info)); Person People

9 cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); } // end of function

10 void GetData(Info &Person,fstream &People) { char buffer[4]; do { cout << “Enter the persons name; cin.getline(Person.Name, 15); do { cout << “Age: ”; cin.getline(buffer, 14); } while (validate(buffer)); // validate function would need to be defined Person.age=atoi(buffer); People.write((char *)&Person, sizeof (Info)); cout << “Do you want to “ << “enter another record?”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); } Person People

11 cout << “Enter the persons name; cin.getline(Person.Name, 15); do { cout << “Age: ”; cin.getline(buffer, 14); } while (validate(buffer)); Person.age=atoi(buffer); People.write((char *)&Person, sizeof (Person)); cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); Person Adams

12 cout << “Enter the persons name; cin.getline(Person.Name, 15); do { cout << “Age: ”; cin.getline(buffer, 14); } while (validate(buffer)); Person.age=atoi(buffer); People.write((char *)&Person, sizeof (Person)); cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); Person Adams 21

13 cout << “Enter the persons name; cin.getline(Person.Name, 15); do { cout << “Age: ”; cin.getline(buffer, 14); } while (validate(buffer)); Person.age=atoi(buffer); People.write((char *)&Person, sizeof (Person)); cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); Person Adams 21

14 cout << “Enter the persons name; cin.getline(Person.Name, 15); do { cout << “Age: ”; cin.getline(buffer, 14); } while (validate(buffer)); Person.age=atoi(buffer); People.write((char *)&Person, sizeof (Person)); cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); Person Davis Adams 21

15 cout << “Enter the persons name; cin.getline(Person.Name, 15); do { cout << “Age: ”; cin.getline(buffer, 14); } while (validate(buffer)); Person.age=atoi(buffer); People.write((char *)&Person, sizeof (Person)); cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); Person Davis 24 Adams 21

16 cout << “Enter the persons name; cin.getline(Person.Name, 15); do { cout << “Age: ”; cin.getline(buffer, 14); } while (validate(buffer)); Person.age=atoi(buffer); People.write((char *)&Person, sizeof(Person)); cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); Person Davis 24 Adams 21 Davis 24

17 cout << “Enter the persons name; cin.getline(Person.Name, 15); do { cout << “Age: ”; cin.getline(buffer, 14); } while (validate(buffer)); Person.age=atoi(buffer); People.write((char *)&Person, sizeof (Person)); cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); Person Smith Adams 21 Davis 24

18 cout << “Enter the persons name; cin.getline(Person.Name, 15); do { cout << “Age: ”; cin.getline(buffer, 14); } while (validate(buffer)); Person.age=atoi(buffer); People.write((char *)&Person, sizeof (Person)); cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); Person Smith 18 Adams 21 Davis 24

19 cout << “Enter the persons name; cin.getline(Person.Name, 15); do { cout << “Age: ”; cin.getline(buffer, 14); } while (validate(buffer)); Person.age=atoi(buffer); People.write((char *)&Person, sizeof (Person)); cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); Person Smith 18 Adams 21 Davis 24 Smith 18

20 cout << “Do you want to enter another record? ”; cin >> Again; cin.ignore( ); } while (toupper (Again) == ‘Y’); Person Smith 18 Adams 21 Davis 24 Smith 18

21 struct Info // Modified from textbook { char Name[15]; int age; }; int main() {fstream People; char Again; People.open(“people.dat”, ios::in | ios::binary); if (People == 0) { cout << “Error opening file. Program aborting.\.”; return; } cout << “Here are the “ << “people in the file:\n\n”; ReadFile(People); return 0; } Adams 21 Davis 24 Smith 18 P

22 void ReadFile(fstream &P) { Info Person; P.read((char *) &Person, sizeof(Info)); while (!P.eof( )) { cout << “Name: ”; cout << Person.Name << endl; cout << “Age: ”; cout << Person.age << endl; cout << “\nStrike any key to see the next record.\n”; cin.get(Again); P.read((char *)&Person, sizeof(Person)); } cout << “That’s all the” << “ information in the file!\n; P.close( ); } Adams 21 Davis 24 Smith 18

23 while (!P.eof( )) { cout << “Name: ”; cout << Person.Name << endl; cout << “Age: ”; cout << Person.age << endl; cout << “\nStrike any key to see the next record.\n”; cin.get(Again); P.read((char *)&Person, sizeof(Person));} cout << “That’s all the information” << “in the file!\n;” P.close( ); } Adams 21 Davis 24 Smith 18

24 while (!P.eof( )) { cout << “Name: ”; cout << Person.Name << endl; cout << “Age: ”; cout << Person.age << endl; cout << “\nStrike any key to see the next record.\n”; cin.get(Again); P.read((char *)&Person, sizeof(Person)); } cout << “That’s all the information” << “in the file!\n”; P.close( ); } Adams 21 Davis 24 Smith 18

25 while (!P.eof( )) { cout << “Name: ”; cout << Person.Name << endl; cout << “Age: ”; cout << Person.age << endl; cout << “\nStrike any key to see the next record.\n”; cin.get(Again); P.read((char *)&Person, sizeof(Person)); } cout << “That’s all the information” << “ in the file!\n”; P.close( ); } Adams 21 Davis 24 Smith 18

26 while (!P.eof( )) { cout << “Name: ”; cout << Person.Name << endl; cout << “Age: ”; cout << Person.age << endl; cout << “\nStrike any key to see the next record.\n”; cin.get(Again); P.read((char *)&Person, sizeof(Person)) } cout << “That’s all the information” <<“ in the file!\n”; P.close( ); } Adams 21 Davis 24 Smith 18

27 int main() // Using an array {fstream People; char Again; int count = 0; Info Persons[20]; People.open(“people.dat”, ios::in | ios::binary); if (People == 0) {cout << “Error opening file.” << “Program aborting.\.”; return; } cout << “Here are the “ << “people in the file:\n\n”; ReadFile(People,Persons,count); } Adams 21 Davis 24 Smith 18

28 void ReadFile(fstream &P, Info a[], int &count) { P.read((char *) &a[count], sizeof(Info)); while (!P.eof( )) {count++; P.read((char *)&a[count], sizeof(Info)); } P.close( ); } Adams 21 Davis 24 Smith 18

29 Creating a Random access File File.seekp(32L, ios::beg); –Sets the write position to the 33rd byte (byte 32) from the beginning of the file. File.seekp(-10L, ios::end); Sets the write position to the 11th byte (byte 10) from the end of the file. P. 736

30 Creating a Random access File File.seekp(120L, ios::curr) –Sets the write position to the 121st byte (byte 120) from the current position File.seekg(2L, ios::beg); Sets the read position to the 3rd byte (byte 2) from the beginning of the file. P. 736

31 Creating a Random access File File.seekg(-100L, ios::end); –Sets the read position to the 101st byte (byte 100) from the end of the file. File.seekg(40L, ios::cur); Sets the read position to the 41st byte (byte 40) from the current position. P. 736

32 Creating a Random access File File.seekg(0L, ios::end); –Sets the read position to the end of the file. P. 736

33 #include using namespace std; int main () { fstream File(“letters.txt”, ios::in); char Ch; File.seekg(5L, ios::beg); File.get(Ch); cout<<“Byte 5 from beginning: “<< Ch<< endl; File.seekg(-10L, ios::end); File.get(Ch); cout << “Byte 10 from end: “ << Ch << endl; File.seekg(3L, ios::cur); File.get(Ch); cout << “Byte 3 from current: ” << Ch << endl; File.close( ); return 0; } abcdefghijklm P. 737 Pgrm 12-17

34 #include using namespace std; int main () { fstream File(“letters.txt”, ios::in); char Ch; File.seekg(5L, ios::beg); File.get(Ch); cout<<“Byte 5 from beginning: “<< Ch<< endl; File.seekg(-10L, ios::end); File.get(Ch); cout << “Byte 10 from end: “ << Ch << endl; File.seekg(3L, ios::cur); File.get(Ch); cout << “Byte 3 from current: ” << Ch << endl; File.close( ); return 0; } abcdefghijklm P. 737 Pgrm 12-17

35 #include using namespace std; int main () { fstream File(“letters.txt”, ios::in); char Ch; File.seekg(5L, ios::beg); File.get(Ch); cout<<“Byte 5 from beginning: “<< Ch<< endl; File.seekg(-10L, ios::end); File.get(Ch); cout << “Byte 10 from end: “ << Ch << endl; File.seekg(3L, ios::cur); File.get(Ch); cout << “Byte 3 from current: ” << Ch << endl; File.close( ); return 0; } abcdefghijklm P. 737 Pgrm 12-17

36 #include using namespace std; int main () { fstream File(“letters.txt”, ios::in); char Ch; File.seekg(5L, ios::beg); File.get(Ch); cout<<“Byte 5 from beginning: “<< Ch<< endl; File.seekg(-10L, ios::end); File.get(Ch); cout << “Byte 10 from end: “ << Ch << endl; File.seekg(3L, ios::cur); File.get(Ch); cout << “Byte 3 from current: ” << Ch << endl; File.close( ); return 0; } abcdefghijklm P. 737 Pgrm 12-17

37 #include //Declaration of Invtry structure struct Invtry { char Desc[31]; int Qty; float Price; };

38 int main() {fstream Inventory(“Invtry.dat”, ios::out | ios::binary); Invtry Record = { “ ”, 0, 0.0 }; //Now write the blank records for (int Count = 0; Count < 5; Count++) {cout << “Now writing record” << Count << endl; Inventory.write((char *)&Record, sizeof(Record)); } Inventory.close( ); return 0; } \ P. 744 Pgrm 12-20

39 int main() {fstream Inventory(“Invtry.dat”, ios::in | ios::out | ios::binary); Invtry Record; long RecNum; cout <<“Which record do you want” <<“to edit?”; cin >> RecNum; Inventory.seekg(RecNum * sizeof (Record), ios::beg); Inventory.read((char *) &Record, sizeof(Record)); \ P. 746 Pgrm Record

40 cout << “Description: ”; cout << Record.Desc << endl; cout << “Quantity: ”; cout << Record.Qty << endl; cout << Price: ”; cout << Record.Price << endl; \ P. 747 Pgrm 12-22

41 cout << “Enter the new data:\n; cout << Description: ”; cin.ignore( ); cin.getline(Record.Desc, 31); cout << “Quantity: ”; cin >> Record.Qty; cout << “Price: ”; cin >> Record.Price; Inventory.seekp(RecNum * sizeof (Record), ios::beg); Inventory.write((char *) &Record, sizeof(Record)); Inventory.close( ); return 0; } \ P. 747 Pgrm Record

42 cout << “Enter the new data:\n; cout << Description: ”; cin.ignore( ); cin.getline(Record.Desc, 31); cout << “Quantity: ”; cin >> Record.Qty; cout << “Price: ”; cin >> Record.Price; Inventory.seekp(RecNum * sizeof (Record), ios::beg); Inventory.write((char *) &Record, sizeof(Record)); Inventory.close( ); return 0; } \ TV P. 747 Pgrm Record

43 cout << “Enter the new data:\n; cout << Description: ”; cin.ignore( ); cin.getline(Record.Desc, 31); cout << “Quantity: ”; cin >> Record.Qty; cout << “Price: ”; cin >> Record.Price; Inventory.seekp(RecNum * sizeof (Record), ios::beg); Inventory.write((char *) &Record, sizeof(Record)); Inventory.close( ); return 0; } \ TV P. 747 Pgrm Record

44 cout << “Enter the new data:\n; cout << Description: ”; cin.ignore( ); cin.getline(Record.Desc, 31); cout << “Quantity: ”; cin >> Record.Qty; cout << “Price: ”; cin >> Record.Price; Inventory.seekp(RecNum * sizeof (Record), ios::beg); Inventory.write((char *) &Record, sizeof(Record)); Inventory.close( ); return 0; } \ TV P. 747 Pgrm Record

45 cout << “Enter the new data:\n; cout << Description: ”; cin.ignore( ); cin.getline(Record.Desc, 31); cout << “Quantity: ”; cin >> Record.Qty; cout << “Price: ”; cin >> Record.Price; Inventory.seekp(RecNum * sizeof (Record), ios::beg); Inventory.write((char *) &Record, sizeof(Record)); Inventory.close( ); return 0; } \ TV \ TV P. 747 Pgrm Record

46 File Operations #include //Declare a structure for the record. struct Info { char Name[51]; int Age; char Address1[51]; char Address2[51]; char Phone[14]; };

47 File Operations using printer int main() { ofstream Printer(“prn”); fstream People(“people.dat”, ios::in); Info Person; char Again; if (People == 0) {cout << Error opening file. Program aborting.\n”; return; }

48 People.read((char *)&Person, sizeof(Person)); while (!People.eof( )) { Printer << Person.Name << endl; Printer << Person.Age << endl; Printer << Person.Address1 << endl; Printer << Person.Address2 << endl; Printer << Person.Phone << endl << endl; People.read((char *)&Person, sizeof(Person)); } People.close( ); return 0; }

49 Q & A

STARTING OUT WITH STARTING OUT WITH Conclusion of Class 16