Difference between revisions of "Q2 A binary file question"
(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…') |
|||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
− | |||
− | |||
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.<br> | 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.<br> | ||
Write the following function:<br> | Write the following function:<br> | ||
− | '''void Process(fstream& fs, int recsize);'''<br> | + | '''void Process(fstream& fs, int recsize);'''<br> |
− | |||
− | |||
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> | 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.<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.<br> |
Latest revision as of 17:38, 4 August 2010
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);
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.