Open main menu

CDOT Wiki β

Q2 A binary file question

Revision as of 17:38, 4 August 2010 by Catherine.leung (talk | contribs) (Created page with ' 0 false 18 pt 18 pt 0 0 false false false ASCII files on dos/windows store both CR (ascii 13) and LF(ascii 10) to indi…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
                 0  false      18 pt  18 pt  0  0    false  false  false                           

ASCII files on dos/windows store both CR (ascii 13) and LF(ascii 10) to indicate the end of a line of text. On unix only LF is used. As a result, when you transfer text files from unix to dos machines (or vice versa) you need to set the mode to ascii or the resulting platform would not be able to process the data correctly.

Write the following function:

void Process(fstream& fs, int recsize);
0 false 18 pt 18 pt 0 0 false false false


This function is passed an instance of fstream fs. fs is associated with a file that is open in binary mode for both reading and writing (so don't try to open the file because it is already open). The second parameter recsize is the size (number of bytes) of one record in the file associated with fs. The last two bytes of each record is always CR and LF respectively. Your function needs to read out each record then write it back into the file such that the CR at the end of each record is removed.</br>

NOTE: it is important that you only remove the CR from the end of the records. If they occur elsewhere within the record you should ignore it.