CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Jan 2003
    Location
    The Volunteer State - USA
    Posts
    90

    Writes OK but doesn't Read very well?

    Hi Boys 'n Girls-
    Kin someone point out the problem with this code?

    #include "stdafx.h"
    #include <stdio.h>

    int main(int argc, char* argv[])
    {
    FILE *Afile;
    int xx=123456,yy;
    Afile=fopen("d:Test.txt","wb");
    fwrite(&xx,4,1,Afile);
    rewind (Afile);
    fread(&yy,4,1,Afile);
    printf("\n Wrote: %d\n Read: %d\n",xx,yy);
    _fcloseall();
    return 0;
    }
    Thanks,
    --------------------------------------------
    http://www.volfirst.net/~sirjorj/index.html
    ---------------------------------------------

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Well...what is the problem? I simply assume - based on your subject - that the write command has not been flushed to the file at the time you are reading again...so you might want to add a 'fflush(Afile)' after your write statement...

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    you open with the mode : "wb"

    I'm not really that familiar with the C open modes, but I think
    that it should be : "r+b" or "w+b"
    Last edited by Philip Nicoletti; February 10th, 2004 at 04:49 PM.

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by Philip Nicoletti
    you open with the mode : "wb"

    I'm not really that familiar with the C open modes, but I think
    that it should be : "r+b" or "w+b"
    No..."wb" is actually fine...you are referring to a different mode...there exist two modes starting with a 'r':
    • "r" -> opens file for reading
    • "r+" -> opens file for reading and changing

    Thus, both "rb" and "r+b" are valid - though different - operations...same applies to writing...

  5. #5
    Join Date
    Feb 2004
    Location
    Portland, OR
    Posts
    13
    To read and write, you must open the file as either "r+" or "w+". The binary mode (no translation of carriage return/line feed) is probably irrelavent in this case (though "r+b" or "w+b" would be correct).

    In your case, the "wb" only opened the file for writing and not reading.

    Further, when opened this way, writes/reads are not neccessarily committed when you do a rewind(), so you need to do a flush. Here is some info from MSDN:

    >>
    When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for "update"). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired.

    >>
    b
    Open in binary (untranslated) mode; translations involving carriage-return and linefeed characters are suppressed.

    Link:
    http://msdn.microsoft.com/library/de...c_._wfopen.asp
    Andrew Ferlitsch

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by aferlitsch
    In your case, the "wb" only opened the file for writing and not reading.
    Overlooked the obvious...

    Well, I guess it is too late for me...

  7. #7
    Join Date
    Jan 2003
    Location
    The Volunteer State - USA
    Posts
    90
    No- its not too late!
    Sorry folks I made a mistake in setting up an example situation of the real problem. The integer is actually being read in as an element of a larger char block.
    A more correct example program would go like this:

    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    int main(int argc, char* argv[])
    {
    FILE *Afile;
    int xx=123456,yy;
    char aa[4];
    Afile=fopen("d:Test.txt","w+b");
    fwrite(&xx,4,1,Afile);
    rewind (Afile);
    fread(aa,4,1,Afile);
    yy=(int)aa;
    printf("\n Wrote: %d\n Read: %d\n",xx,yy);
    _fcloseall();
    return 0;
    }
    ..and thanx for all the replies.
    --------------------------------------------
    http://www.volfirst.net/~sirjorj/index.html
    ---------------------------------------------

  8. #8
    Join Date
    Jun 2002
    Posts
    395
    Code:
    yy=(int)aa;
    You are setting yy to be equal to a pointer to aa, not the bytes that are in aa. You would need to do something like

    Code:
    yy = (int) (*(int *)aa);
    I can't remember off the top of my head the byte-ordering that Intel processors use, but you might run into byte ordering problems doing this. Why not just do:

    Code:
    fread(&yy,4,1,Afile);

  9. #9
    Join Date
    Jul 2001
    Location
    jordan
    Posts
    63
    you can try memcpy(&yy, a, 4)
    not sure the bytes will still mean what you want them to mean

    awni

  10. #10
    Join Date
    Jan 2003
    Location
    The Volunteer State - USA
    Posts
    90
    Originally posted by wayside

    Why not just do:

    Code:
    fread(&yy,4,1,Afile);
    'Casue I don't know the file address for yy but I do know its location in a buffer that had been read.
    So I must handle it from that location.
    --------------------------------------------
    http://www.volfirst.net/~sirjorj/index.html
    ---------------------------------------------

  11. #11
    Join Date
    Jan 2003
    Location
    The Volunteer State - USA
    Posts
    90
    [QUOTE]Originally posted by wayside
    Code:
    yy=(int)aa;
    You are setting yy to be equal to a pointer to aa, not the bytes that are in aa. You would need to do something like

    Code:
    yy = (int) (*(int *)aa);
    Bravo wayside!
    Your code did the trick.
    {Haven't tested awni's memcpy)

    Thanks for the help.
    --------------------------------------------
    http://www.volfirst.net/~sirjorj/index.html
    ---------------------------------------------

  12. #12
    Join Date
    Jun 2002
    Posts
    395
    Originally posted by Sirjorj
    'Casue I don't know the file address for yy but I do know its location in a buffer that had been read.
    So I must handle it from that location.
    I don't understand what you mean by the "file address", this makes no sense to me.

    By doing

    Code:
    fread(&yy,4,1,Afile);
    you are using the yy variable, which you have declared as an int, a a 4 byte buffer, the same as you are using with your char aa[4] variable. They both take the same amount of space, by using &yy you are getting a pointer to it and can treat it like a 4 byte buffer, and read directly into it. You don't need to go through a char buffer first.

  13. #13
    Join Date
    Jan 2003
    Location
    The Volunteer State - USA
    Posts
    90
    [QUOTE]Originally posted by wayside
    I don't understand what you mean by the "file address", this makes no sense to me.

    Yes you are right with regard to the SAMPLE code I submitted, I could read the value directly.
    BUT in my REAL program I would not be able to set fseek()...
    so I must take the value from an available buffer as it is being processed.

    Guess I should have explained the difference between my sample and the real-life problem.
    But- tnx again.

    P.S. the memcpy() also works OK.
    --------------------------------------------
    http://www.volfirst.net/~sirjorj/index.html
    ---------------------------------------------

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