Presentation is loading. Please wait.

Presentation is loading. Please wait.

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)

Similar presentations


Presentation on theme: "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)"— Presentation transcript:

1 Binary Files

2 Text Files vs. Binary Files Text files: A way to store data in a secondary storage device using Text representation (e.g., ASCII characters) Binary files: A way to store data in a secondary storage device using the same binary representation as main memory.

3 Binary vs. Text ASCII Table

4 Data Records Generally, when reading binary files, we read (or write) records. A file pointer is used to track the current position in the file. The current position is measured as a byte offset from the beginning of the file.

5 Random Access Random access files allow the manipulation of the file pointer to read & write data from different locations within the file. The next read or write will take place starting at the current location of the file pointer (fp) fp

6 Random Access Text files only allow sequential access Data must be read in the order it is stored, starting at the beginning of the file. Binary files allow sequential & random access.

7 Sequential Access When the current position of a binary file pointer is the beginning of a file, a read (or write) will advance the file pointer to the next record. fp 0 1 2 3

8 Random Access However, the file pointer can also be placed randomly within the file, allowing access to specific records at any place in the file (similar to using arrays). fp 0 1 2 3

9 fstream With text files, we have declared streams as an ifstream or ofstream. With binary files is possible to declare simply an fstream and then open files for input, output, or both… fstream instr, outstr, inoutstr; instr.open(“infile.txt”, ios::in); outstr.open(“outfile.txt”, ios::out); inoutstr.open(“file.txt”, ios::in | ios::out); Bitwise OR

10 The Mode Parameter ios::in This indicates that the stream will be used for input. ios::out This indicates that the stream will be used for output. ios::binary This causes the file to be accessed as a binary file. If you need to specify more than one of them simply place a vertical bar between them.

11 file.clear() It is possible to read a file over and over (you will do this in program 5). You should be aware that there is a problem if during any read, you reach the end-of-file marker. This sets a flag that remains set the next time you try to read the file again. To clear the eof flag, we use if(file.eof( )) file.clear( );

12 FYI This is how you can reuse (i/o)fstreams… ifstream fin; fin.open("first.txt");... fin.close(); fin.clear(); fin.open("second.txt");...

13 Placing the file pointer The file pointer moves throughout the file in offsets (the size of a single record). Various arguments and fstream functions are associated with the file pointer: sizeof() fstr.seekg() fstr.tellg() ios::beg ios::end ios::cur

14 . seekg() Moves the file pointer to another location within the file. ios::beg location (in bytes) from the beginning of the file. ios::cur location (in bytes) from the current file pointer position. ios::end location (in bytes) from the end of the file.

15 sizeof( ); When moving through binary files, you must move in record size leaps. sizeof( ) is a function to return the size of an argument (parameter). sizeof(int) will return the number of bytes necessary to store an integer (4 on our compiler). sizeof(myObject) will return the byte size of the declared object.

16 .seekg( ) –.seekp( ).seekp( ) can only be used with single directional streams. Use.seekg( ) for files opened for both input and output. These functions are used to move the file pointer within the file. Two parameters are required 1) the number of bytes to move 2) the position to start measuring from

17 fstream fstr; fstr.seekg(); To place the file pointer at the beginning of the file we could use: fstr.seekg(0, ios::beg); To move the file pointer to the end of the file use: fstr.seekg(0, ios::end);

18 int main() { int x; fstream infile; infile.open("file.dat", ios::bin | ios::in); infile.seekg(9, ios::beg); // move 9 bytes from the beginning of the file … infile.seekg(-10, ios::cur); //back up 10 bytes from current position … infile.seekg(0,ios::end); //move to the end of the file infile.close(); }

19 To advance the pointer n records from the beginning of the file use: fstr.seekg(n * sizeof(record), ios::beg); fp 0 1 2 3 fstr.seekg(3 * sizeof(record), ios::beg); fstr.seekg(3 * 30, ios::beg); fstr.seekg(90, ios::beg); 30 bytes 0 29|30 59|60 89|90

20 To move back one record from the current location use: fstr.seekg(-sizeof(record), ios::cur); fp 0 1 2 3 30 bytes 0 29|30 59|60 89|90 fp

21 Practice How would you place the pointer… n records from the end of a file fstr.seekg(-n * sizeof(record), ios::end); n records past the current position fstr.seekg(n * sizeof(record), ios::cur); pointing to the last record in a file fstr.seekg(-sizeof(record), ios::end);

22 .tellg( ); This function will return the current byte location of the pointer. The number of bytes from the beginning of the file. You can use this to find the number of records in a file using: fstr.seekg(0, ios::end); numRec = fstr.tellg()/sizeof(record);

23 I/O with Binary Files First you must open an fstream It must be opened for both input and output AND binary mode fstream inOut; inOut.open("myFile.bin", ios::binary | ios::in | ios::out); if(!inOut.is_open( )) // same as inOut.fail( ) { cout << "error opening myFile.bin" << endl; exit(1); }

24 read( ) – write ( ) read( ) and write( ) are fstream functions that perform I/O with a byte stream. Each takes the address of a buffer and the number of bytes to transfer (count).

25 I/O Buffers The buffer is an array of characters that stores the data as it is sent or received from a device. Non-character data types require a type conversion, using the (char*) type cast. This tells the compiler to consider the buffer as an array of characters (regardless of its contents).

26 Example fstream fstr; int intData; int arr[20]; // array of 20 integers fstr.read((char*) &intData, sizeof(int)); fstr.write((char*) arr, sizeof(arr));

27 program 5 In program 5 you will not be reading the entire file into your program. You will read a single record at a time, use it, update it, or delete it. You will use the index.txt file to determine where records are in the binary file. The Index class will help you do this

28 Update a Player 1. Find the player’s name in the index file and thus the location index associated with that player. 2. Use the index to move the file pointer to the desired record. 3. Read the Player record and update the data. 4. Move back one record in the file.(Why?) 5. Write the updated record to the file.

29 More Command-Line Arguments

30 c:\>a.out PlayerRecord.bin index.txt int main(int argc, char *argv[]) argc = 3 argv[0] = a.out argv[1] = PlayerRecord.bin argv[2] = index.txt

31 Opening Files to open the first file for input and the second file for output we could use ifstream first; inInv.open(argv[i]); ofstream second; outInv.open(argv[2]); or fstream first, second; first.open(argv[1], ios::in); second.open(argv[2], ios::out);


Download ppt "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)"

Similar presentations


Ads by Google