fgetc Reading 65535 is 255?

This is a discussion on fgetc Reading 65535 is 255? within the C++ Programming forums, part of the General Programming Boards category; So I've started writing my assembler now was just doing some little tests to determine the best way to read ...

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    fgetc Reading 65535 is 255?

    So I've started writing my assembler now was just doing some little tests to determine the best way to read / write the words (unsigned short's typedef'd).

    So far I have this:

    Code:
    int main(int argc, char* argv[])
    {
        FILE* f = fopen("PROGRAM.lvmp", "w");
        word instruction[3] = {0};
    
        cout << "instruction: ";
        cin >> instruction[0];
        cin >> instruction[1];
        cin >> instruction[2];
    
        fputc(instruction[0], f);
        fputc(instruction[1], f);
        fputc(instruction[2], f);
    
        fclose(f);
    
        f = fopen("PROGRAM.lvmp", "r");
    
        instruction[0] = fgetc(f);
        instruction[1] = fgetc(f);
        instruction[2] = fgetc(f);
        fclose(f);
    
        cout << "opcode: " << instruction[0] << "\n";
        cout << "op1: " << instruction[1] << "\n";
        cout << "op2: " << instruction[2] << endl;
        return 0;
    }
    It works fine unless I enter something like

    Code:
    3 10 65531
    (movl 10, r0)

    In which case when fgetc reads it I get this output:

    Code:
    opcode: 3
    op1: 10
    op2: 251
    If I give it 65535 (OP_END) op2 is read as 255. I can only guess that this is happening due to something to do with ASCII. How could I get around this? fgetc / fputc is probably not the best choice anyway.

    Thoughts?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    > fputc(instruction[2], f);
    My thought is that you should write the LSB and MSB of your word.

    You should also probably use "rb" and "wb"

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Noted.

    EDIT:

    >> My thought is that you should write the LSB and MSB of your word.

    Sorry, can you explain why and how?

    EDIT2: Actually don't tell me (yet. Perhaps) I've found a few articles and think I might have it in a minute.

    EDIT3: Well now I know why, but I'm still a bit stuck on how to actually do it.
    Last edited by cboard_member; 03-12-2006 at 10:52 AM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    Like
    fputc(instruction[0] & 0xff, f);
    fputc((instruction[0]>>8)&0xff, f);

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I just solved it as I saw your reply

    Here's what I had but looking at yours it's a bit OTT:

    Code:
    int main(int argc, char* argv[])
    {
        FILE* f = fopen("PROGRAM.lvmp", "wb");
        word instruction[3] = {0};
        byte opcodeLSB;
        byte opcodeMSB;
    
        cout << "instruction: ";
        cin >> instruction[0];
    
        opcodeMSB = instruction[0] & 255;
        opcodeLSB = (instruction[0] >> 8) & 255;
    
        fputc(opcodeLSB, f);
        fputc(opcodeMSB, f);
        fclose(f);
    
        f = fopen("PROGRAM.lvmp", "rb");
    
        opcodeMSB = fgetc(f);
        opcodeLSB = fgetc(f);
        instruction[0] = (opcodeMSB << 8) + opcodeLSB;
    
        fclose(f);
    
        cout << "opcode: " << instruction[0] << "\n";
        return 0;
    }
    Cheers to you and http://www.gamedev.net/reference/art...rticle2091.asp
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    Or just use fread() and fwrite() to save the whole array in one shot.

    Or fputwc

  7. #7
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    After all that? Bah, at least I learnt something.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Slight problem with socket reading!!!
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 02-15-2006, 08:55 AM
  2. reading file word by word
    By 98holb in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 04:49 PM
  3. reading from a file... fgetc??
    By demi222 in forum C Programming
    Replies: 13
    Last Post: 10-14-2005, 04:32 PM
  4. Reading from a file
    By Wiz_Nil in forum C++ Programming
    Replies: 1
    Last Post: 02-26-2002, 08:31 AM
  5. problem reading files in C
    By angelfly in forum C Programming
    Replies: 9
    Last Post: 10-10-2001, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21