English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 05 October 2012, 09:33   #1
pmc
gone
 
pmc's Avatar
 
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();
any ideas what I'm doing wrong and why I get rogue characters at the end of an otherwise perfectly written text file?
pmc is offline  
Old 05 October 2012, 11:25   #2
Leffmann
 
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);
and if you ever do C-style I/O then you would add "b" to the file mode:
Code:
FILE* f = fopen("i.bin", "rb");
FILE* f = fopen("o.bin", "wb");
Leffmann is offline  
Old 05 October 2012, 11:35   #3
pmc
gone
 
pmc's Avatar
 
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;
And set my input text file to say: this is a test

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.
pmc is offline  
Old 05 October 2012, 11:49   #4
pmc
gone
 
pmc's Avatar
 
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;}
Massive thanks to CaptainHug/RSE - you're a legend mate!
pmc is offline  
Old 05 October 2012, 17:36   #5
Leffmann
 
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.
Leffmann is offline  
 


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 20:15.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.06903 seconds with 15 queries