Thread: Problem of read file in C++

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

    Problem of read file in C++

    I have .raw files which is generated by Photoshop.

    And I now want to read the values in .raw files to my C++ program and then
    do some calculation on it.

    Then I would like to ask two things. What is the data structure of a .raw file?
    (RGB? A 2D array or whatever?)
    And can anybody please give me some brief idea on how to read the file content?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Like what?

    How to read binary files - fopen("file.raw", "rb" );

    How to interpret all the bytes?
    Maybe here - http://www.wotsit.org/

    Maybe type "photoshop raw file format" into a search engine.

    Maybe create something simple, like a 16x16 image containing 3 primary colour squares, say 8x8 (a bit like the MS logo) and save that in RAW format.
    Then look at that file using a hex editor.
    Make very small changes and match that to changes in the file.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    I used the following code to read a raw file:

    Code:
    const int size = 1080*1080;
    int r;
    	
    unsigned char*  A;
    A = (unsigned char*)malloc(size*sizeof(char));
    
    fin.open("test.raw",ios::binary);	// read data file of four integers
    for(r=0; r<size; r++)	// into array
    fin >> A[r];
    fin.close();	
    	
    ofstream fout;
    fout.open("result.raw",ios::binary);
    // write data file
    for(r=0; r<size; r++){	
    	fout << A[r];
    }
    
    fout.close();
    So, this is just a roundtrip that import the raw file into the array A, and then export data into the file result.raw. However, the image output is incomplete. What's the problem with my coding?
    Last edited by rosicky2005; 11-27-2006 at 12:43 AM.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You're using formatted I/O (the >> operator) for binary data. That's the most obvious problem. With binary data, use read() and write().
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

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. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM