CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2009
    Posts
    7

    Question re: fread/fwrite

    hello all,

    I have a unsigned char 1d array of size_x, let's call it Array1. (It contains information on intensity of a grey scale graph). Before saving it to a file, i can go Array1[245] and obtain the intensity value at that position of the array. Then I save it this way:

    FILE * the_file;
    the_file = fopen(filename,"w+b")
    fwrite(Array1,sizeof(unsigned char),size_x,the_file);


    so now the saved file should contain all these information right?

    then I tried to read the same file back

    unsigned char * read_mat;
    file_read = fopen(filename, "rb" );
    fseek (file_read , 0 , SEEK_END);
    file_size = ftell (file_read);
    rewind (file_read);
    read_mat = new unsigned char[file_size];
    fread(read_mat,sizeof(unsigned char),file_size,file_read);

    now should the read_mat array contain the same information as Array1 the one i used to save my file with?

    because I tried to just read a random number from read_mat[] but it always gives me zero.
    ..this is one way i did:
    CString temp_text;
    for (int i=0;i<file_size;i++)
    {
    int temp_num=(int)read_mat[i];
    temp_text.Format("int is %d",temp_num);
    MessageBox(temp_text);
    }

    did something go wrong with saving? or something go wrong with reading?
    the file I saved shows the expected size.

  2. #2
    Join Date
    Feb 2005
    Location
    Pune (India)
    Posts
    644

    Thumbs up Re: Question re: fread/fwrite

    hi,


    try to print your array before writing to the file.. ! have you checked the array contents. ???


    -Anant
    "Devise the simplest possible solution that solves the problems"

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Question re: fread/fwrite

    Does the binary file contain the correct information? You can check it with a hex viewer for instance. Both fread and fwrite are returning the number of bytes successfully read or written. Check that and compare with the actual number of bytes you expect.

    BTW, if you write in C++, you could use ifstream/ofstream to read/write and vector<unsigned char> to hold the array.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Jan 2009
    Posts
    7

    Re: Question re: fread/fwrite

    I've used a hex viewer to verify my message, it looks perfectly fine. The same size as I've expected it to be. there are valid data within it.

    I've also printed out the array before saving and it was perfectly fine. So the file itself is not the problem. Just how I read it out and how I print out the read file seems to be the problem.

    i've checked the read/written byte and they agree with each other.

  5. #5
    Join Date
    Feb 2005
    Location
    Pune (India)
    Posts
    644

    Thumbs up Re: Question re: fread/fwrite

    Hi,


    FILE * the_file;
    the_file = fopen(filename,"w+b")
    fwrite(Array1,sizeof(unsigned char),size_x,the_file);
    fclose(the_file );



    either close file before opening again or use same handle to read contents....from file i.e alraedy open file handle !


    -Anant
    "Devise the simplest possible solution that solves the problems"

  6. #6
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Question re: fread/fwrite

    No need to close file. It's ok to have several open handles to the same file. Try calling fflush before calling fread though.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured