file handling basics n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
File handling Basics PowerPoint Presentation
Download Presentation
File handling Basics

Loading in 2 Seconds...

play fullscreen
1 / 75

File handling Basics - PowerPoint PPT Presentation


  • 149 Views
  • Uploaded on

File handling Basics. Lecture 7. Stream Classes. A stream is a general name given to a flow of data. In C++ a stream is represented by an object of a particular class. So far we’ve used the cin and cout stream objects.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

File handling Basics


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
    Presentation Transcript
    1. File handling Basics Lecture 7

    2. Stream Classes • A stream is a general name given to a flow of data. • In C++ a stream is represented by an object of a particular class. • So far we’ve used the cin and cout stream objects. • Different streams are used to represent different kinds of data flow. • For example, the ifstream class represents data flow from input disk files.

    3. What is a File? • A file is a collection of information, usually stored on a computer’s disk. Information can be saved to files and then later reused.

    4. File Names • All files are assigned a name that is used for identification purposes by the operating system and the user. • e.g; • Abc.txt • Abc.cpp • Abc.exe • Abc.html • Abc.bat • Abc.dat

    5. The Process of Using a File • Using a file in a program is a simple three-step process • The file must be opened. If the file does not yet exits, opening it means creating it. • Information is then saved to the file, read from the file, or both. • When the program is finished using the file, the file must be closed.

    6. Figure 1

    7. Figure 2

    8. Writing Data #include <fstream> #include <iostream> #include <string> using namespace std; int main() { char ch = ‘a’; int j = 100; double d = 26.12; string str1 = “BS”; string str2 = “CS”; ofstreamoutfile(“c:\\fdata.txt”);

    9. outfile << ch << j << “ ” << d << str1 << ‘ ‘ << str2; cout << “File written\n”; return 0; }

    10. Reading Data #include <fstream> #include <iostream> #include <string> using namespace std; int main() { char ch; int j; double d; string str1; string str2; ifstreaminfile(“fdata.txt”);

    11. infile >> ch >> j >> d >> str1 >> str2; cout << ch << endl << j << endl << d << endl << str1 << endl << str2 << endl; return 0; OUTPUT: a 100 26.12 BS CS

    12. Setting Up a Program for File Input/Output • Before file I/O can be performed, a C++ program must be set up properly. • File access requires the inclusion of fstream.h

    13. Opening a File • Before data can be written to or read from a file, the file must be opened. ifstreaminputFile; inputFile.open(“customer.dat”);

    14. Program // This program demonstrates the declaration of an fstream // object and the opening of a file. #include <iostream.h> #include <fstream.h> #include <string.h> void main(void) { fstream dataFile; // Declare file stream object char fileName[20]; cout << "Enter the name of a file you wish to open\n"; cout << "or create: "; cin.getline(fileName, 20); dataFile.open(fileName, ios::out); cout << "The file " << fileName << " was opened.\n"; }

    15. Program Output with Example Input Enter the name of a file you wish to open or create: mystuff.dat [Enter] The file mystuff.dat was opened.

    16. Table

    17. The iosClass • The ios class is the grandfather of all the stream classes, and contains the majority of the features those need to operate C++ streams.

    18. Table of ios features

    19. Opening a File at Declaration fstreamdataFile(“names.dat”, ios::in | ios::out | ios::app); • we use ios::app because we want to preserve whatever was in the file before and whatever we write to the file will be added following the existing contents • We use ios::in and ios::out because we want to perform both input and output on the file. • we use ios::binary because we’re writing binary objects. • The vertical bars between the flags cause the bits representing these flags to be logically combined into a single integer, so that several flags can apply simultaneously.

    20. Program // This program demonstrates the opening of a file at the // time the file stream object is declared. #include <iostream.h> #include <fstream.h> void main(void) { fstream dataFile(“c:\\mcs2\\names.dat", ios::out); cout << "The file names.dat is open.\n"; }

    21. Program // This program demonstrates the close function. #include <iostream.h> #include <fstream.h> void main(void) { fstream dataFile; dataFile.open("testfile.txt", ios::out); if (!dataFile) { cout << "File open error!" << endl; return; } Else { cout << "File was created successfully.\n"; cout << "Now closing the file.\n"; dataFile.close(); } }

    22. Using << to Write Information to a File • The stream insertion operator (<<) may be used to write information to a file. dataFile<< “Mansoor hates C++ programming !”

    23. Program // This program uses the << operator to write information to a file. #include <iostream.h> #include <fstream.h> void main(void) { fstream dataFile; dataFile.open(“c:\\bcs2\\demofile.txt", ios::out | ios::app); if (!dataFile) { cout << "File open error!" << endl; }

    24. Program continues cout << "File opened successfully.\n"; cout << "Now writing information to the file.\n"; dataFile<< “Ali\n"; dataFile << “Jamal\n"; dataFile << “Kamran\n"; dataFile << “Jawad\n"; dataFile.close(); cout << "Done.\n"; }

    25. Program Screen Output File opened successfully. Now writing information to the file. Done. Output to File demofile.txt Ali Jamal kamran jawad

    26. Program // This program writes information to a file, closes the file, // then reopens it and appends more information. #include <iostream.h> #include <fstream.h> void main(void) { fstream dataFile; dataFile.open("demofile.txt", ios::out); dataFile << “Ali\n"; dataFile << “Jamal\n"; dataFile.close(); dataFile.open("demofile.txt", ios::app); dataFile << “Kamran\n"; dataFile << “Jawad\n"; dataFile.close(); }

    27. Output to File demofile.txt Ali Jamal kamran Jawad

    28. Program #include <iostream.h> #include <fstream.h> #include <iomanip.h> void main(void) { fstream outFile("table.txt", ios::out); int nums[3][3] = { 2897, 5, 837, 34, 7, 1623, 390, 3456, 12 }; // Write the three rows of numbers for (int row = 0; row < 3; row++) { for (int col = 0; col < 3; col++) { outFile << setw(4) << nums[row][col] << " "; } outFile << endl; } outFile.close(); }

    29. Contents of File TABLE.TXT 2897 5 837 34 7 1623 390 3456 12

    30. Using >> to Read Information from a File • The stream extraction operator (>>) may be used to read information from a file.

    31. Program // This program uses the >> operator to read information from a file. #include <iostream.h> #include <fstream.h> void main(void) { fstream dataFile; char name[81]; dataFile.open("demofile.txt", ios::in); if (!dataFile) { cout << "File open error!" << endl; return; } cout << "File opened successfully.\n"; cout << "Now reading information from the file.\n\n";

    32. Program continues for (int count = 0; count < 4; count++) { dataFile >> name; cout << name << endl; } dataFile.close(); cout << "\nDone.\n"; }

    33. Program Screen Output File opened successfully. Now reading information from the file. Ali Jamal Kamran Jawad Done.

    34. Detecting the End of a File • The eof() member function reports when the end of a file has been encountered. if (inFile.eof()) inFile.close();

    35. Program // This program uses the file stream object's eof() member // function to detect the end of the file. #include <iostream.h> #include <fstream.h> void main(void) { fstream dataFile; char name[81]; dataFile.open("demofile.txt", ios::in); if (!dataFile) { cout << "File open error!" << endl; return; } cout << "File opened successfully.\n"; cout << "Now reading information from the file.\n\n";

    36. Program continues dataFile >> name; // Read first name from the file while (!dataFile.eof()) { cout << name << endl; dataFile >> name; } dataFile.close(); cout << "\nDone.\n"; }

    37. Program Screen Output File opened successfully. Now reading information from the file. Ali jamal kamran jawad Done.

    38. Passing File Stream Objects to Functions • File stream objects may be passed by reference to functions. boolopenFileIn(fstream &file, char name[51]) { bool status; file.open(name, ios::in); if (file.fail()) status = false; else status = true; return status; }

    39. More Detailed Error Testing • All stream objects have error state bits that indicate the condition of the stream.

    40. Table

    41. Table

    42. Program // This program demonstrates the return value of the stream // object error testing member functions. #include <iostream.h> #include <fstream.h> // Function prototype void showState(fstream &); void main(void) { fstream datafile("stuff.dat", ios::out); if (dataFile.fail()) { cout << "cannot open the file.\n"; }

    43. Program continues int num = 10; cout << "Writing to the file.\n"; testFile << num; // Write the integer to testFile showState(testFile); testFile.close(); // Close the file testFile.open("stuff.dat", ios::in); // Open for input if (testFile.fail()) { cout << "cannot open the file.\n"; return; }

    44. Program continues cout << "Reading from the file.\n"; testFile >> num; // Read the only number in the file showState(testFile); cout << "Forcing a bad read operation.\n"; testFile >> num; // Force an invalid read operation showState(testFile); testFile.close(); // Close the file } // Definition of function ShowState. This function uses // an fstream reference as its parameter. The return values of // the eof(), fail(), bad(), and good() member functions are // displayed. The clear() function is called before the function // returns.

    45. Program continues void showState(fstream &file) { cout << "File Status:\n"; cout << " eof bit: " << file.eof() << endl; cout << " fail bit: " << file.fail() << endl; cout << " bad bit: " << file.bad() << endl; cout << " good bit: " << file.good() << endl; file.clear(); // Clear any bad bits }

    46. Program Output Writing to the file. File Status: eof bit: 0 fail bit: 0 bad bit: 0 good bit: 1 Reading from the file. File Status: eof bit: 0 fail bit: 0 bad bit: 0 good bit: 1 Forcing a bad read operation. File Status: eof bit: 1 fail bit: 2 bad bit: 0 good bit: 0

    47. Member Functions for Reading and Writing Files • File stream objects have member functions for more specialized file reading and writing.

    48. Program // This program uses the file stream object's eof() member // function to detect the end of the file. #include <iostream.h> #include <fstream.h> void main(void) { fstream nameFile; char input[81]; nameFile.open(“student.txt", ios::in); if (!nameFile) { cout << "File open error!" << endl; return; }