Thread: Problem of read file in C++(hexagonal resp)

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    25

    Problem of read file in C++(hexagonal resp)

    I have some problem reading data from a file

    The format of the file is a series of number with hexagonal representation :

    e.g. 1A6F3D...............

    In theory, the method should be like this:

    fopen(“xxxxxx.raw”, “rb”);

    because binary is used. Then I should use byte as unit to read the data to my array of datatype unsigned char.

    However, I don't know how to do this by coding.

    Can anyone please help me?

  2. #2
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Is it actually binary data, as you presented us with a hexadecimal represented textually right there.

    >> Then I should use byte as unit to read the data to my array of datatype unsigned char.

    Does this data have any meaning? We honestly don't know whether to interpret that data in nibbles, bytes, words, etc. it's really kind of diffcult to help regardless of whether it's binary or text without this knowledge. More details on the file format are needed.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    Quote Originally Posted by Tonto
    Is it actually binary data, as you presented us with a hexadecimal represented textually right there.

    >> Then I should use byte as unit to read the data to my array of datatype unsigned char.

    Does this data have any meaning? We honestly don't know whether to interpret that data in nibbles, bytes, words, etc. it's really kind of diffcult to help regardless of whether it's binary or text without this knowledge. More details on the file format are needed.
    The file is a raw image file, which the colour data is represented as RGBRGB.....without any header and dimension specified. My task is to read the data and use the data to do some image processing. And finally put the result back as image.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    If that is like R8G8B8 then you could do something like

    Code:
    unsigned char r, g, b;
    FILE * file = fopen(“xxxxxx.raw”, “rb”);
    
    fread(&r, 1, 1, file);
    fread(&g, 1, 1, file);
    fread(&b, 1, 1, file);

  6. #6
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    AFAIK, RGB values was in unsigned char for each of them (0-255). So, why don't you loop and read for 3 chars until you can't read no more?

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    Here is my piece of code to do brightness processing with raw file :

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main () {
      int length;
      char * buffer;
      char * target;
      int  * int_buffer; 
      int  * temp;
      ifstream is;
      ofstream os;
    
      is.open ("try.raw", ios::binary );
    
      // get length of file:
      is.seekg (0, ios::end);
      length = is.tellg();
      is.seekg (0, ios::beg);
    
      // allocate memory:
      buffer = new char [length];
      target = new char [length];
      int_buffer = new int [length];
      
      
      // read data as a block:
      is.read (buffer,length);
    
      is.close();
      
      //read memory into a integer array to do brightness processing
      for(int i=0;i<length;i++){
    	  int_buffer[i] = buffer[i];
    	  int_buffer[i] += 256;
      }
    
     //process and clipping value done here
       
    for(int i=0;i<length;i++){
    	  int_buffer[i] += 20;
    
    	  if(int_buffer[i]>255)
    		  int_buffer[i] = 255;
    	  
    	  if(int_buffer[i]<0)
    		  int_buffer[i] = 0;
      }
      
      //read the value done into a char array
    for(int i=0;i<length;i++){
      
    		target[i] = int_buffer[i];
      }
      
      //write file
      os.open("target.raw", ios::binary);
    
      os.write(target,length);
    
      os.close();
    
      system("pause");
      return 0;
    }
    However, strange image is processed. What's wrong with my code?
    Because of the nature of my research topic. I must convert the char array to integer array, and then
    turn back to char array again.
    Last edited by rosicky2005; 12-11-2006 at 09:39 PM.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Code:
    int main () {
       string New_Name;
       std::cout << " \n";
       std::cout << "Enter New File Name: ";
       std::cin >> New_Name;
    
    ifstream fin("try.raw", ios::in | ios::binary | ios::beg );
      if (fin.bad())
        {
       return 0;
         }
    
       fin.seekg( 0, ios::end ) ;
       int size = fin.tellg()   ;
       fin.seekg( 0, ios::beg ) ;
      
       char buffer [size];
       fin.read( buffer, size) ;
    
       buffer[size] = 0 ;
       string Raw_File(buffer, size) ;
    
      char* OutPutName = new char[New_Name.length()];
      for( int i = 0; i <New_Name.length(); i++ )  
      OutPutName[i] = New_Name[i];
      OutPutName[New_Name.length()] = '\0';
    
      strcat (OutPutName, ".raw");
      
      ofstream fout(OutPutName, ios::out | ios::binary);
      fout << Raw_File << std::endl;
    
       fin.close();
       fout.close();
    
       return 0;
      }
    I'm not very good at this coding stuff, what I've learned is from other kind souls, but that oughta copy the file, byte for byte.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    Quote Originally Posted by Oldman47
    I'm not very good at this coding stuff, what I've learned is from other kind souls, but that oughta copy the file, byte for byte.
    But how to copy byte for byte?

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Err, I don't get what you're saying. I pretty much gave what I know, hope someone else can give you a better helping hand.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well first of all, you should be using an unsigned char for reading stuff out of the file.

    If it's a signed char on your machine, then anything in the range 0x80 to 0xFF is going to appear to be negative.

    > int_buffer[i] += 256;
    Ok, but doesn't that make if(int_buffer[i]>255) a useless test, since it will always be true?

    On a simple test, did you check that the input file and output file were the same length?


    Before you start messing about with files, check the brightness algorithm out on something like
    Code:
    const unsigned char old[] = { 0x00, 0x44, 0x88, 0xCC, 0xEE, 0xF8 };
    // apply algorithm and print results
    Until that produces the required answers, using a file is just an unnecessary complication.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Read File Problem
    By lonewolf367 in forum C Programming
    Replies: 11
    Last Post: 11-30-2005, 10:32 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM