250 likes | 398 Views
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:
E N D
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: ifstreaminFile; • Declares output file stream object named outFile to be object of ofstream class: ofstreamoutFile;
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.
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.
Opening and Closing Files • When using fstream class’s open() method, two arguments required: file’s external name and mode indicator.
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.
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.
Writing to Files • Open the weather.txt file in any text editor to confirm that the data was written.
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.
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.
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.
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.
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.
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*.
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.