05 October 2012, 09:33 | #1 |
gone
Join Date: Apr 2007
Location: completely gone
Posts: 1,596
|
Rogue characters on file write
Morning boys (and girls )
Bet you never thought you'd see me posting to this forum of coder's heaven, eh? In an effort to do a bit of learning I've been converting my little software cipher to C++. However, I've hit a bit of an issue. Everything works fine *except* I seem to get rogue characters tacked onto the end of the text that I write out to a file. Here's the relevant loop in my code: Code:
ifstream inputFile(inFile, ios::in); if (!inputFile) { output_to_console(input_file_open_err_msg); return 0;} ofstream outputFile(outFile, ios::out); if (!outputFile) { output_to_console(output_file_open_err_msg); return 0;} while (!inputFile.eof()) { inputFile.get(in_char); out_char = perplexity_cipher(in_char); outputFile.put(out_char); if (!outputFile.good()) { output_to_console(output_file_write_err_msg); return 0;} } inputFile.close(); outputFile.close(); |
05 October 2012, 11:25 | #2 |
Join Date: Jul 2008
Location: Sweden
Posts: 2,269
|
I think what you're seeing is the automatic line ending conversion. Windows tries to be different and incompatible with everything else, and uses carriage return + line feed for line endings (byte sequence 13 and 10).
If you open your files in binary mode it should work: Code:
// ios::in and ios::out are implied with ifstream and ofstream ifstream inputFile(inFile, ios::binary); ofstream outputFile(outFile, ios::binary); Code:
FILE* f = fopen("i.bin", "rb"); FILE* f = fopen("o.bin", "wb"); |
05 October 2012, 11:35 | #3 |
gone
Join Date: Apr 2007
Location: completely gone
Posts: 1,596
|
No go I'm afraid
Same problem using binary mode rather than text mode I changed the call to the cipher function so instead I just go: Code:
out_char = in_char; what I get back is a text file saying: this is a testt ie. The last actual character from the input file gets repeated and tacked on the end of the output file. |
05 October 2012, 11:49 | #4 |
gone
Join Date: Apr 2007
Location: completely gone
Posts: 1,596
|
This fixes it:
Code:
while (inputFile.good()) { inputFile.get(in_char); if (inputFile.eof()) { break;} |
05 October 2012, 17:36 | #5 |
Join Date: Jul 2008
Location: Sweden
Posts: 2,269
|
Ok that was it I had no idea the EOF flag didn't update until after an I/O op. Keep the text mode in mind as well, it will both discard CR bytes as you read and insert new ones as you write. C++ streamed I/O is a real minefield.
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Rogue Trooper Level 2 | CodyJarrett | support.Games | 2 | 14 September 2017 21:05 |
Write .hdf file to SCSI drive for Amiga? | rewoffl | support.WinUAE | 3 | 02 September 2012 14:00 |
Rogue Trooper v1.1 WHD | Retroplay | request.Old Rare Games | 0 | 06 August 2010 21:45 |
Maps for Space Rogue? | Korodny | request.Old Rare Games | 0 | 09 April 2004 21:56 |
how do i decompress from one drive and write the file to another drive? | sgt_chimp | support.Apps | 5 | 15 December 2001 18:42 |
|
|