473,770 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fread not reading all information

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(ulFile Size+1);
memset(pBuffer, '\0',ulFileSize +1);
fread(pBuffer, ulFileSize,1, inStream);
fseek(inStream, 0, SEEK_SET);
writeLogMessage ("Read copied Globals:
\n============= =============== =====");
writeLog(pBuffe r, ulFileSize);

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

printed to logfile:
Read copied Globals:
=============== =============== ===
g_iNumGraphs 0
g_iNumUndef 0
g_iAnalysisUnde f 0
iNumber 3163
g_szOutFilename Results61101.tx t
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_i NumGraphs\t4\t% d\n",g_iNumGrap hs);
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 '07 #1
3 4595
[followups to comp.lang.c]
ju********@gmai l.com 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(ulFile Size+1);
memset(pBuffer, '\0',ulFileSize +1);
fread(pBuffer, ulFileSize,1, inStream);
fseek(inStream, 0, SEEK_SET);
writeLogMessage ("Read copied Globals:
\n============= =============== =====");
writeLog(pBuffe r, ulFileSize);

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

printed to logfile:
Read copied Globals:
=============== =============== ===
g_iNumGraphs 0
g_iNumUndef 0
g_iAnalysisUnde f 0
iNumber 3163
g_szOutFilename Results61101.tx t
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_i NumGraphs\t4\t% d\n",g_iNumGrap hs);
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.

Apr 12 '07 #2

"red floyd" <no*****@here.d udewrote in message
news:RS******** *********@newss vr27.news.prodi gy.net...
[followups to comp.lang.c]
ju********@gmai l.com 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(ulFile Size+1);
memset(pBuffer, '\0',ulFileSize +1);
fread(pBuffer, ulFileSize,1, inStream);
fseek(inStream, 0, SEEK_SET);
writeLogMessage ("Read copied Globals:
\n============ =============== ======");
writeLog(pBuffe r, ulFileSize);

input file "globals":
g_iNumGraphs 4
<-----this is missing below
g_iNumUndef 0
g_iAnalysisUnd ef 0
iNumber 3163
g_szOutFilenam e Results61101.tx t
gStart 1176399755
gFileName
genfilename ../GenDistance/GenDist3163.txt <-------this is
missing below

printed to logfile:
Read copied Globals:
============== =============== ====
g_iNumGraphs 0
g_iNumUndef 0
g_iAnalysisUnd ef 0
iNumber 3163
g_szOutFilenam e Results61101.tx t
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_iNumGra phs);
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

Apr 12 '07 #3
red floyd wrote:
ju********@gmai l.com 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(ulFile Size+1);
memset(pBuffer, '\0',ulFileSize +1);
fread(pBuffer, ulFileSize,1, inStream);
fseek(inStream, 0, SEEK_SET);
writeLogMessage ("Read copied Globals:
\n============ =============== ======");
writeLog(pBuffe r, ulFileSize);

input file "globals":
g_iNumGraphs 4
<-----this is missing below
g_iNumUndef 0
g_iAnalysisUnd ef 0
iNumber 3163
g_szOutFilenam e Results61101.tx t
gStart 1176399755
gFileName
genfilename ../GenDistance/GenDist3163.txt <-------this is
missing below

printed to logfile:
Read copied Globals:
============== =============== ====
g_iNumGraphs 0
g_iNumUndef 0
g_iAnalysisUnd ef 0
iNumber 3163
g_szOutFilenam e Results61101.tx t
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_iNumGra phs);
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.securityfoc us.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

Apr 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2486
by: RootShell | last post by:
Dear All Im having some dificulty here: I found a great PHP code by Catalin Mihaila that reads a SRC (Sinclair Spectrum $SCREEN Image Format) and tranforms it into PNG format to show onscreen by the PHP gd2 Library. The problem is that im trying to translate the 'fread' structure into a MySQL blob reading, as follows:
5
2095
by: syntax | last post by:
hi here is my test code include<stdio.h> #include<stdlib.h> int main() {
2
618
by: Josh Wilson | last post by:
The fread() is what I believe is having trouble, but I do not know why. I know that the temp file is created and written to (and w/ the appropriate amount of data). fread() continues to return 0, but I don't know why it shouldn't work; but when I used ferror(stdin) and it gives 0 suggesting to me it read w/o problem, so I'm confused since I know there is information in the file, and it is allegedly reading from stdin, any help please? ...
6
19462
by: Patrice Kadionik | last post by:
Hi all, I want to make a brief comparison between read() and fread() (under a Linux OS). 1. Family read and Co: open, close, read, write, ioctl... 2. Family fread and Co: fopen, fclose, fread, swrite, fcntl... Family read and Co:
8
17192
by: M. Åhman | last post by:
I'm reading "C: A Reference Manual" but still can't understand a very basic thing: is there any functional difference between fgetc/fputc and fread/fwrite (when reading/writing one unsigned char)? 1. Books and documentation on C tell me fread and fwrite is "binary". What does binary mean in this? Anything to do with opening a file in binary mode? 2. I'm also sure I've read somewhere on the net that fread and
20
7550
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file frequently(forwards and backwards) and do fread() from that offset. Or better still, could anyone let me know some good ways to achieve what I need to do as above?Can I get hold of the file and being able to read in without using fread()? Using...
8
11948
by: vippstar | last post by:
-- foo.c -- #include <stdio.h> int main(void) { struct { int a; int b; } foo; fread(&foo, sizeof foo, 1, stdin); return 0; } -- foo.c --
2
5843
by: steve005 | last post by:
Hi, I am writing a binary file with matlab that consists of a couple hundred double values and then reading it in with c++. The problem arises in c++ when I try and read in the files. At a specific point it stops and wont read any more values. I use this code to open it and read it: if( (err = fopen_s( &fp,"C:/simfile1.tmg0" , "rb" )) != 0 ); fread(&coneAngle,sizeof(double),1,fp); After about twenty simmilar reads it fails, returns an...
15
3187
by: =?ISO-8859-15?Q?L=E9na=EFc?= Huard | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all, For some reasons, somewhere in a program, I'd like, if possible, to quickly parse a whole file before rewinding it and letting the full analysis start. My problem is that the FILE* I want do parse has been fopen'ed far away from where I am and I don't know in which MODE my FILE* has been opened. And additionally, my FILE* may not be a regular file, but a continuous
0
9618
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10259
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.