fread not reading all information

J

juleigha27

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
 
R

red floyd

[followups to comp.lang.c]
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.
 
M

Malcolm McLean

red floyd said:
[followups to comp.lang.c]
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.
 
C

CBFalconer

red said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top