Thread: File access problem

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    6

    File access problem

    Hello.
    I'm planning to code a level editor for an old dungeon game, and thought it'd be nice to output the map file contents to the screen to see what's going on.

    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    int main()
    {
          ifstream inFile;
          inFile.open("mod0.map");
         
          // skip header  
          inFile.seekg(56);
          
          unsigned char test;
         
          // output "map" of the corridor
          for(int count = 0; count < 24; count++)
         {	
                inFile >> test;
                if(test < 10)
                      cout << 0;
                cout << int(test) << ' ';
          }
    
          inFile.close();
          return(0);
    }
    This results in the following output:
    02 04 00 00 00 00 00 00 00 00 06 06 00 00 00 00 00 00 00 00 06 04 203 06

    Looking at the relevant section in the file using a hex editor gives:
    02 04 00 00 00 00 00 00 00 00 0B 06 0B 06 00 00 00 00 00 00 00 00 06 04

    Why is it missing the 0x0Bs out? The output I'd expect is:
    02 04 00 00 00 00 00 00 00 00 11 06 11 06 00 00 00 00 00 00 00 00 06 04

    Any ideas would be much appreciated. I know that 0x0B is a control code, but wouldn't casting it to an int make it display as 11? Thanks.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Try opening the file as a binary file.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    6
    Sorry, I should have mentioned that I tried that already and it didn't work! I thought this would be really simple, but it's starting to irritate me now

  4. #4
    Unregistered
    Guest
    try using int instead of char

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You have to use read() to read a binary file. And as Adrianxw says, open with the binary flag.
    Code:
       unsigned char test;
       ifstream inFile;
       inFile.open("mod0.map",ios::binary);
       inFile.read(&test,1);
    
    //or read it all at one time with:
       unsigned char test[24];
       ifstream inFile;
       inFile.open("mod0.map",ios::binary);
       inFile.read(test,24);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM