fread not reading all information

Discussion in 'C++' started by juleigha27@gmail.com, Apr 12, 2007.

  1. Guest

    Hi,

    First off, I want to apologize if this already got posted it seemed
    like something happened when I tried to post it previously and it
    didn't work.
    I am new to file manipulation with c. I am trying to read in a file
    and then parse it to assign the value to variables. I have read that
    fread is more is better for this then fscanf, so I tried the following
    code with the input file and resulting output:

    inStream = fopen("globals", "r")))
    fseek(inStream,0,SEEK_END);
    ulFileSize = ftell(inStream);
    fseek(inStream,0,SEEK_SET);
    char *pBuffer = (char *)malloc(ulFileSize+1);
    memset(pBuffer, '\0',ulFileSize+1);
    fread(pBuffer, ulFileSize,1, inStream);
    fseek(inStream, 0, SEEK_SET);
    writeLogMessage("Read copied Globals:
    \n=================================");
    writeLog(pBuffer, ulFileSize);

    input file "globals":
    g_iNumGraphs 4
    <-----this is missing below
    g_iNumUndef 0
    g_iAnalysisUndef 0
    iNumber 3163
    g_szOutFilename Results61101.txt
    gStart 1176399755
    gFileName
    genfilename ../GenDistance/GenDist3163.txt <-------this is
    missing below



    printed to logfile:
    Read copied Globals:
    =================================
    g_iNumGraphs 0
    g_iNumUndef 0
    g_iAnalysisUndef 0
    iNumber 3163
    g_szOutFilename Results61101.txt
    gStart 1176399755
    gFileName
    genfilename


    What I found is that some things aren't read for instance like the 4
    on the first line. I encountered similar problems with fscanf when I
    attempted to implement this. I am going about this incorrectly? Can
    anyone suggest a better solution? For the first line I also inserted
    an additional tab and typed 4 for example:
    fprintf(fp,"g_iNumGraphs\t4\t%d\n",g_iNumGraphs);
    g_iNumGraphs 4 4
    With fread if reads the first 4, but not the second (which was
    originally written to the file as a variable)
    ie This what results after fread or fscanf :
    g_iNumGraphs 4 0


    I would greatly appreciate any tips!
    Thanks,
    Jules
    , Apr 12, 2007
    #1
    1. Advertising

  2. red floyd Guest

    [followups to comp.lang.c]
    wrote:
    > Hi,
    >
    > First off, I want to apologize if this already got posted it seemed
    > like something happened when I tried to post it previously and it
    > didn't work.
    > I am new to file manipulation with c. I am trying to read in a file
    > and then parse it to assign the value to variables. I have read that
    > fread is more is better for this then fscanf, so I tried the following
    > code with the input file and resulting output:
    >
    > inStream = fopen("globals", "r")))
    > fseek(inStream,0,SEEK_END);
    > ulFileSize = ftell(inStream);
    > fseek(inStream,0,SEEK_SET);
    > char *pBuffer = (char *)malloc(ulFileSize+1);
    > memset(pBuffer, '\0',ulFileSize+1);
    > fread(pBuffer, ulFileSize,1, inStream);
    > fseek(inStream, 0, SEEK_SET);
    > writeLogMessage("Read copied Globals:
    > \n=================================");
    > writeLog(pBuffer, ulFileSize);
    >
    > input file "globals":
    > g_iNumGraphs 4
    > <-----this is missing below
    > g_iNumUndef 0
    > g_iAnalysisUndef 0
    > iNumber 3163
    > g_szOutFilename Results61101.txt
    > gStart 1176399755
    > gFileName
    > genfilename ../GenDistance/GenDist3163.txt <-------this is
    > missing below
    >
    >
    >
    > printed to logfile:
    > Read copied Globals:
    > =================================
    > g_iNumGraphs 0
    > g_iNumUndef 0
    > g_iAnalysisUndef 0
    > iNumber 3163
    > g_szOutFilename Results61101.txt
    > gStart 1176399755
    > gFileName
    > genfilename
    >
    >
    > What I found is that some things aren't read for instance like the 4
    > on the first line. I encountered similar problems with fscanf when I
    > attempted to implement this. I am going about this incorrectly? Can
    > anyone suggest a better solution? For the first line I also inserted
    > an additional tab and typed 4 for example:
    > fprintf(fp,"g_iNumGraphs\t4\t%d\n",g_iNumGraphs);
    > g_iNumGraphs 4 4
    > With fread if reads the first 4, but not the second (which was
    > originally written to the file as a variable)
    > ie This what results after fread or fscanf :
    > g_iNumGraphs 4 0
    >
    >
    > I would greatly appreciate any tips!
    > Thanks,
    > Jules


    This is a C problem, not a C++ problem. You're more likely to get a
    relevant answer on comp.lang.c. Followups set to comp.lang.c.
    red floyd, Apr 12, 2007
    #2
    1. Advertising

  3. "red floyd" <> wrote in message
    news:RSwTh.7993$...
    > [followups to comp.lang.c]
    > wrote:
    >> Hi,
    >>
    >> First off, I want to apologize if this already got posted it seemed
    >> like something happened when I tried to post it previously and it
    >> didn't work.
    >> I am new to file manipulation with c. I am trying to read in a file
    >> and then parse it to assign the value to variables. I have read that
    >> fread is more is better for this then fscanf, so I tried the following
    >> code with the input file and resulting output:
    >>
    >> inStream = fopen("globals", "r")))
    >> fseek(inStream,0,SEEK_END);
    >> ulFileSize = ftell(inStream);
    >> fseek(inStream,0,SEEK_SET);
    >> char *pBuffer = (char *)malloc(ulFileSize+1);
    >> memset(pBuffer, '\0',ulFileSize+1);
    >> fread(pBuffer, ulFileSize,1, inStream);
    >> fseek(inStream, 0, SEEK_SET);
    >> writeLogMessage("Read copied Globals:
    >> \n=================================");
    >> writeLog(pBuffer, ulFileSize);
    >>
    >> input file "globals":
    >> g_iNumGraphs 4
    >> <-----this is missing below
    >> g_iNumUndef 0
    >> g_iAnalysisUndef 0
    >> iNumber 3163
    >> g_szOutFilename Results61101.txt
    >> gStart 1176399755
    >> gFileName
    >> genfilename ../GenDistance/GenDist3163.txt <-------this is
    >> missing below
    >>
    >>
    >>
    >> printed to logfile:
    >> Read copied Globals:
    >> =================================
    >> g_iNumGraphs 0
    >> g_iNumUndef 0
    >> g_iAnalysisUndef 0
    >> iNumber 3163
    >> g_szOutFilename Results61101.txt
    >> gStart 1176399755
    >> gFileName
    >> genfilename
    >>
    >>
    >> What I found is that some things aren't read for instance like the 4
    >> on the first line. I encountered similar problems with fscanf when I
    >> attempted to implement this. I am going about this incorrectly? Can
    >> anyone suggest a better solution? For the first line I also inserted
    >> an additional tab and typed 4 for example:
    >> fprintf(fp,"g_iNumGraphs\t4\t%d\n",g_iNumGraphs);
    >> g_iNumGraphs 4 4
    >> With fread if reads the first 4, but not the second (which was
    >> originally written to the file as a variable)
    >> ie This what results after fread or fscanf :
    >> g_iNumGraphs 4 0
    >>
    >>
    >> I would greatly appreciate any tips!
    >> Thanks,
    >> Jules

    >
    > This is a C problem, not a C++ problem. You're more likely to get a
    > relevant answer on comp.lang.c. Followups set to comp.lang.c.
    >

    fread() is designed for binary mode and you are using it on a file opened in
    text. This shouldn't be your problem but it indicates that the approach is
    wrong. For instance if text characters are sometimes represented by two
    bytes then you will have spare nuls at the end of your file - I wondered if
    they might be garbage backspaces, but in fact you zero fill so this can't
    happen.

    The other thing that might be going wrong is that you might pass too many
    characters to fread() because of the translation and problems with ftell(),
    it fails, and for some reason doesn't copy the last few bytes to the file.
    You can test this by checking the return value from fread().
    However these aren't too likely and the rest of the code seems OK. I suspect
    the writeLog() function.
    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Apr 12, 2007
    #3
  4. CBFalconer Guest

    red floyd wrote:
    > wrote: *** [followups to comp.lang.c] ***
    >>
    >> First off, I want to apologize if this already got posted it
    >> seemed like something happened when I tried to post it previously
    >> and it didn't work.
    >>
    >> I am new to file manipulation with c. I am trying to read in a
    >> file and then parse it to assign the value to variables. I have
    >> read that fread is more is better for this then fscanf, so I tried
    >> the following code with the input file and resulting output:
    >>
    >> inStream = fopen("globals", "r")))
    >> fseek(inStream,0,SEEK_END);
    >> ulFileSize = ftell(inStream);
    >> fseek(inStream,0,SEEK_SET);
    >> char *pBuffer = (char *)malloc(ulFileSize+1);
    >> memset(pBuffer, '\0',ulFileSize+1);
    >> fread(pBuffer, ulFileSize,1, inStream);
    >> fseek(inStream, 0, SEEK_SET);
    >> writeLogMessage("Read copied Globals:
    >> \n=================================");
    >> writeLog(pBuffer, ulFileSize);
    >>
    >> input file "globals":
    >> g_iNumGraphs 4
    >> <-----this is missing below
    >> g_iNumUndef 0
    >> g_iAnalysisUndef 0
    >> iNumber 3163
    >> g_szOutFilename Results61101.txt
    >> gStart 1176399755
    >> gFileName
    >> genfilename ../GenDistance/GenDist3163.txt <-------this is
    >> missing below
    >>
    >> printed to logfile:
    >> Read copied Globals:
    >> =================================
    >> g_iNumGraphs 0
    >> g_iNumUndef 0
    >> g_iAnalysisUndef 0
    >> iNumber 3163
    >> g_szOutFilename Results61101.txt
    >> gStart 1176399755
    >> gFileName
    >> genfilename
    >>
    >> What I found is that some things aren't read for instance like
    >> the 4 on the first line. I encountered similar problems with
    >> fscanf when I attempted to implement this. I am going about this
    >> incorrectly? Can anyone suggest a better solution? For the first
    >> line I also inserted an additional tab and typed 4 for example:
    >>
    >> fprintf(fp,"g_iNumGraphs\t4\t%d\n",g_iNumGraphs);
    >> g_iNumGraphs 4 4
    >> With fread if reads the first 4, but not the second (which was
    >> originally written to the file as a variable)
    >> ie This what results after fread or fscanf :
    >> g_iNumGraphs 4 0
    >>
    >> I would greatly appreciate any tips!

    >
    > This is a C problem, not a C++ problem. You're more likely to get
    > a relevant answer on comp.lang.c. Followups set to comp.lang.c.


    No you didn't. You sent two copies to c.l.c.

    --
    <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
    <http://www.securityfocus.com/columnists/423>
    <http://www.aaxnet.com/editor/edit043.html>

    "A man who is right every time is not likely to do very much."
    -- Francis Crick, co-discover of DNA
    "There is nothing more amazing than stupidity in action."
    -- Thomas Matthews



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Apr 13, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vincent Vollers
    Replies:
    0
    Views:
    556
    Vincent Vollers
    May 22, 2005
  2. syntax

    reading a file using fread()

    syntax, Feb 23, 2004, in forum: C Programming
    Replies:
    3
    Views:
    8,456
    Richard Bos
    Feb 24, 2004
  3. Fernando Barsoba

    fread() function and reading binary files

    Fernando Barsoba, Oct 17, 2005, in forum: C Programming
    Replies:
    2
    Views:
    392
    Martin Ambuhl
    Oct 17, 2005
  4. bill
    Replies:
    4
    Views:
    436
    Barry Schwarz
    Nov 9, 2005
  5. red floyd

    Re: fread not reading all information

    red floyd, Apr 12, 2007, in forum: C Programming
    Replies:
    4
    Views:
    345
    red floyd
    Apr 13, 2007
Loading...

Share This Page