   C++ file I/O
  Hello,I am planning to write an NES emulator, and have started by trying to load a ROM file into memory, but already I'm having some trouble... I read in the data from the file like this:    std::ifstream::pos_type size;    size = file.tellg();    rom = new char[size];    file.seekg(0, std::ios::beg);    file.read(rom, size);    file.close();I then tried writing all the data from memory to a different file, and that worked fine. I tried checking the ROM header in memory, and everything seemed to be in order. But when tried to check the next byte after that, it didn't match what was in the ROM file, which I had look at in a hex editor, and which looked like this:    4E 45 53 1A 08 10 42 00 00 00 00 00 00 00 00 00 <-- the header    98 80 6C 81 40 82 14 83 E8 83 BC 84 90 85 64 86I tried printing it to the screen, and discovered that I had somehow ended up with FFFFFF98 instead of just 98. This had happened with some of the other bytes too:    4E 45 53 1A 08 10 42 00 00 00 00 00 00 00 00 00    FFFFFF98 FFFFFF80 6C FFFFFF81 40 FFFFFF82 14 FFFFFF83 FFFFFFE8    FFFFFF83 FFFFFFBC FFFFFF84 FFFFFF90 FFFFFF85 64 FFFFFF86So, I tried checking for the value FFFFFF98 in memory instead of 98, and that worked, but like I said, when I wrote this to a file, it wrote just 98.Does anyone know what causes this?   
  Obvious question first: are you opening the file in binary mode?   ;  Yes. I opened the file with std::ios::binary (and std::ios::ate to get the size), read everything into memory, and wrote this to a new ROM file, which played just fine in an emulator.Edit: I tried changing some of the bytes in the rom file and it seems this happens to all the bytes with a value of 80 or higher.   ;  How are you printing the file?   ;  You can read all file without getting its size:#include <algorithm>#include <fstream>#include <vector>#include <iterator>#include <sstream>#include <string>...std::vector<char> rom;{  std::ifstream inf("rom.bin", std::ios::binary);  inf >> std::noskipws;  std::copy(std::ifstream_iterator<char>(inf), std::ifstream_iterator<char>(), std::back_inserter(rom));}// or you can do also this{  std::ifstream inf("rom.bin", std::ios::binary);  std::stringstream ss;  ss << inf.rdbuf();  std::string s = ss.str();  rom.assign(s.begin(), s.end());};  The problem here is that char's are signed in your particular compiler (so values with the high-bit set are considered negative) and somewhere along the line they get converted (signed-extended) to larger integers, thus propagating the eight bit across the entire 32-bit integer. Finally they apparently get printed as unsigned 32-bit integers.Okay.. That sounds a bit weird, but it's hard to explain without seeing your printing code.In practice this means that you'll want to cast to unsigned char before printing. At any rate you'll certainly need a firm grasp on two's complement arithmetic if you intend to write a 6502 emulator.   ;  FFFFFF98 is what you get when you convert the char 0x98 to an int. It's sign-extended the highest bit since it's a negative number.;    for(int i(0); i < size; ++i)    {   std::cout << std::hex << (int)rom << " ";    }I tried removing std::hex, and all the numbers with FFFFFF are then shown as negative numbers.   ;  Try casting to unsigned char first, or store the array as unsigned chars to begin with.E.g. std::cout << std::hex << (int) (unsigned char) rom;;  The contents are probably fine. Both mean the value -104 but in different data types.The problem is what how you are printing the contents to screen. You're printing the value as a signed integer which means sign extension is performed. If the upper bit of the byte is set then the value is treated as negative and for it to be negative and the size of an int, sign extension needs to be performed. Thus the value is sign-extended before printing. Either show the code you're using to print the value or cast the char to unsigned char in the print statement.   