Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with fstream::read

Newbie
 
Join Date: Sep 2007
Posts: 21
#1: Jul 19 '08
I'm having a problem with read , my code is somewhat like:
Expand|Select|Wrap|Line Numbers
  1. char chipMemory [chipMemorySize]; // ~35XX
  2. fstream ifile;
  3. ifile.open(argv[1],ios::in|ios::binary|ios::nocreate|ios::ate);
  4. int fileLength=ifile.tellg();
  5. ifile.seekg(ios::beg);
  6. ifile.read(chipMemory,fileLength);
  7. 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)

Member
 
Join Date: Nov 2007
Posts: 46
#2: Jul 22 '08

re: Problem with fstream::read


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


Expand|Select|Wrap|Line Numbers
  1. printf("%d\n", 0x00E0 );
  2. char c = 0x00E0;
  3. printf("%d\n", c);
  4. printf("0x%x\n", c);
  5. printf("0x%x\n", (short)c);
  6.  
Reply


Similar C / C++ bytes