Problem with fstream::read

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aviraldg
    New Member
    • Sep 2007
    • 21

    Problem with fstream::read

    I'm having a problem with read , my code is somewhat like:
    Code:
    char chipMemory [chipMemorySize]; // ~35XX
    fstream ifile;
    ifile.open(argv[1],ios::in|ios::binary|ios::nocreate|ios::ate);
    int fileLength=ifile.tellg();
    ifile.seekg(ios::beg);
    ifile.read(chipMemory,fileLength);
    ifile.close();
    After the last function call in the program, (Note , this is an extract , and seemingly irrelevant pieces have been removed)
    (short) chipMemory[0] "prints as" (printf used) 0xFFFFFFE0
    (short chipMemory[2] "prints as" 0xFFFFFFEE
    it should contain:
    (short) chipMemory[0] == 0x00E0;
    (short) chipMemory[2] == 0x00EE;
    What I'm most puzzled about is that a short is 2 bytes , so the maximum short would be 0xFFFF , not even close to 0xFFFFFFE0.
    I'm guessing this is because the file starts with a nul (0x00) , which is supposed
    to mark the end of a file.
    (Note : I'm not checking for nul in any way at all)
    Last edited by aviraldg; Jul 19 '08, 01:40 AM. Reason: Clarification
  • jabbah
    New Member
    • Nov 2007
    • 63

    #2
    Im not prefectly sure but could it be that 0x00E0 overflows char? should you be using unsigned char?


    [code=c]
    printf("%d\n", 0x00E0 );
    char c = 0x00E0;
    printf("%d\n", c);
    printf("0x%x\n" , c);
    printf("0x%x\n" , (short)c);
    [/code]

    Comment

    Working...