1 / 25

Programming Techniques

Programming Techniques. File Input and Output. Opening and Closing Files. Input file stream objects declared to be of type ifstream Output file stream objects declared to be of type ofstream Declares input file stream object named inFile to be object of ifstream class:

china
Download Presentation

Programming Techniques

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Techniques File Input and Output

  2. Opening and Closing Files • Input file stream objects declared to be of type ifstream • Output file stream objects declared to be of type ofstream • Declares input file stream object named inFile to be object of ifstream class: ifstreaminFile; • Declares output file stream object named outFile to be object of ofstream class: ofstreamoutFile;

  3. Opening and Closing Files • File stream is accessed by its stream object name: one for reading and one writing. • File stream objects must be created and declared in similar manner as variables. • ifstream (input) and ofstream (output) class made available by including fstream header file.

  4. Opening and Closing Files • open() method connect file’s external name to its internal object stream name, only one argument required: external filename. • Connects external text file named data.txt to internal file stream object named outFile: outFile.open(“data.txt”); • Data files stored in separate folders, full path name: outFile.open(“c:\\datafiles\\data.txt”); • Two slashes separate folder names and filenames.

  5. Opening and Closing Files • When using fstream class’s open() method, two arguments required: file’s external name and mode indicator.

  6. Opening and Closing Files • fail() method returns true if file opened unsuccessfully (true the open failed): if (outFile.fail()) { // Do whatever. } • A file is closed by using close() method. • close() method takes no argument.

  7. Writing to Files • fstream file object opened in append mode means existing file is available for data to be added to the end of the file. • If file doesn’t exist, new file with the designated name is created and made available to receive output from the program. • For writing to a file, the cout object is replaced by the ofstream object name declared in the program.

  8. Writing to Files

  9. Writing to Files

  10. Writing to Files • Open the weather.txt file in any text editor to confirm that the data was written.

  11. Parsing File Input

  12. Parsing File Input

  13. Writing to Binary Files • All C++ compilers provide two general-purpose functions for generating random numbers: • rand() • produces series of random numbers in the range 0  rand()  RAND_MAX, the constant RAND_MAX defined in the cstdlib header file. • srand() • supplies a starting “seed” value for rand() • If srand() or another seeding technique isn’t used, rand() always produces the same series of random numbers.

  14. Writing to Binary Files • Argument to srand() function is a call to time() function with NULLargument. • With this argument, time() function reads computer’s internal clock time in seconds. • srand() function then uses this time, converted to an unsigned int, to initialize the rand() function, which generates random numbers.

  15. Writing to Binary Files • This application wants to limit the range of random numbers to be less than 100. • The easiest way to accomplish this is to use the remainder of dividing a random number by 100. • So if a random number is 32,767, the remainder of dividing that by 100 would be 67. • If the number is 22,584, its modulus would be 84.

  16. Writing to Binary Files

  17. Writing to Binary Files

  18. Writing to Binary Files • Because the file will store binary data, not plain text, you don’t want to use the .txt extension. • You can either omit the extension entirely or use .dat (a three-letter extension representing data). • Within the loop, each element is assigned a random value between 0 and 99.

  19. Writing to Binary Files • Often you need to write data other than characters. • Use reinterpret_cast operator that can cast any pointer type to another pointer type of unrelated classes. • It simply performs binary copy of the value from one type to the other without altering the data.

  20. Writing to Binary Files • Syntax of using reinterpret_cast operator: reinterpret_cast<dataType*>(address) • address is the starting address of the data (primitive, array of object) and dataType is the data type you are casting to. • In this case for binary I/O, it is char*.

  21. Reading from Binary Files • The syntax for read function is streamObject.read(char* address, int size) • size parameter indicates the maximum number of bytes read.

  22. Reading from Binary Files

  23. Reading from Binary Files

  24. Reading from Binary Files

More Related