Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > fread not reading all information

Reply
Thread Tools

fread not reading all information

 
 
juleigha27@gmail.com
Guest
Posts: n/a
 
      04-12-2007
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

 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      04-12-2007
[followups to comp.lang.c]
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      04-12-2007

"red floyd" <(E-Mail Removed)> wrote in message
news:RSwTh.7993$(E-Mail Removed) t...
> [followups to comp.lang.c]
> (E-Mail Removed) 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

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      04-12-2007
red floyd wrote:
> (E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: fread not reading all information red floyd C Programming 4 04-13-2007 03:06 AM
fread() blocks when reading pipe with closed write end bill C Programming 4 11-09-2005 02:18 AM
fread() function and reading binary files Fernando Barsoba C Programming 2 10-17-2005 05:45 AM
fread can not read particular data Meenakshi Matai C Programming 4 06-24-2004 03:40 PM
reading a file using fread() syntax C Programming 3 02-24-2004 06:55 AM



Advertisments