473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Problem with fread()

Helas,

I got something that should be a minor problem, but anyhow it isn't to me
right now.

A little code fragment:
fread(&file_qn, x, 1, fp_q);

The corresponding text file looks like this:
456 5 1.txt%&'
I've already skipped the '456' part with fseek, read the '5' with scanf,
file_qn should now get the '1.txt' part and x is the length of that string,
which is 5.
Thanks for any help in advance !

Alain.
Nov 14 '05 #1
10 4137
On Thu, 29 Jan 2004 05:35:36 +0100, "Alain Lafon" <pr***@gmx.at> wrote
in comp.lang.c:
Helas,

I got something that should be a minor problem, but anyhow it isn't to me
right now.

A little code fragment:
fread(&file_qn, x, 1, fp_q);

The corresponding text file looks like this:
456 5 1.txt%&'
I've already skipped the '456' part with fseek, read the '5' with scanf,
file_qn should now get the '1.txt' part and x is the length of that string,
which is 5.
Thanks for any help in advance !

Alain.


Here's my advice: Post again and explain what your "minor problem"
is. Post a complete compilable code snippet, how the heck are we
supposed to know the type of file_qn or x? If there are compiler
error messages, copy the text and paste into the body of your message.

You haven't provided enough information on your problem for anyone to
offer any other advice.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2
I'm sorry.

Here's my further information:
The code compiles fine. The problem is that the program simply closes when
it should do the fread() part.
Here's the code fragment:

void abhaengen(FILE *fp_q, int key)
{
int i;
const char* file_qn;
-some other variables-
-snip-
fscanf(fp_q, "%i", &x); //gets the size of the string which is to be
read by fread()
fread(&file_qn, x, 1, fp_q); //this is exactly where the program simply
closes with no further messages.
-snip-
}

The file that is to be read looks like this:
456 5 1.txt%&'
I'm sorry to have bothered you with an incomplete posting. But it's already
5:52am and I'm getting a little confused *g*

Thanks again,
Alain.
Nov 14 '05 #3
nrk
Alain Lafon wrote:
I'm sorry.

Here's my further information:
The code compiles fine. The problem is that the program simply closes when
it should do the fread() part.
Here's the code fragment:

void abhaengen(FILE *fp_q, int key)
{
int i;
const char* file_qn;
Why const? This declaration says file_qn is a pointer to const char, which
means you cannot change what file_qn points to. Since this is precisely
what you want to do, lose the const:
char *file_qn;

However, you still haven't made file_qn point to anything useful. One way
of doing this is by using malloc to allocate some memory and make file_qn
point to that.
-some other variables-
-snip-
fscanf(fp_q, "%i", &x); //gets the size of the string which is to be
read by fread()
Now you even know how much memory you need (check the return value of fscanf
to make sure you got a valid integer):
file_qn = malloc(sizeof *file_qn * x); /* check the result for NULL */
fread(&file_qn, x, 1, fp_q); //this is exactly where the program That's totally wrong. fread will read nmemb items each size bytes big into
the area pointed to by ptr:

fread(file_qn, 1, x, fp_q);

Which says, read x items of size 1 from fp_q into the area pointed to by
file_qn. However, note that the fscanf will still leave any spaces between
the integer and the filename in the input stream, which means that simple
fread will get leading blank(s) and part of the filename. You're probably
better off doing this:

fscanf(fp_q, "%*[\n]"); /* throw away any blanks */
fread(&file_qn, x, 1, fp_q); /* now read the filename */
simply
closes with no further messages.
Probably a result of accessing memory you don't own through that fread call.
One would hope a decent OS will give you some indication as to why the
program was killed.

HTH,
-nrk.
-snip-
}

The file that is to be read looks like this:
456 5 1.txt%&'
I'm sorry to have bothered you with an incomplete posting. But it's
already 5:52am and I'm getting a little confused *g*

Thanks again,
Alain.


--
Remove devnull for email
Nov 14 '05 #4
Woo - some magic happened (;
I just made this little steganographic and cryptographic program - it worked
all fine, but i wouldn't be able to handle large filenames. This is why I
started implementing the pointers. I worked like the last 14 hours to get
this thing to work - and you did the very last part of it !

Thank you very, very much !

Btw: I learned that syntax for the fread() function from the book of Herbert
Schidt - somebody who was in the ANSI/ISO comittee - i thought he should
know such things...
Whatever..

Thanks just again !
Nov 14 '05 #5
[..]
void abhaengen(FILE *fp_q, int key)
{
int i;
int x;
const char* file_qn;
-some other variables-
-snip-
fscanf(fp_q, "%i", &x); //gets the size of the string which is to be
read by fread()
"file_qn", when not initialized, is a pointer to an arbitrary location.
If sizeof ( file_qn ) < x, then obviously the behaviour is undefined.

Use:

file_qn = malloc ( (size_t) x );
fread(&file_qn, x, 1, fp_q); //this is exactly where the program simply
closes with no further messages.
-snip-


And, do not forget to free the allocated memory.
[..]
--
Vijay Kumar R Zanvar
Nov 14 '05 #6
Mac
On Thu, 29 Jan 2004 06:33:06 +0100, Alain Lafon wrote:
Woo - some magic happened (;
I just made this little steganographic and cryptographic program - it worked
all fine, but i wouldn't be able to handle large filenames. This is why I
started implementing the pointers. I worked like the last 14 hours to get
this thing to work - and you did the very last part of it !

Thank you very, very much !

Btw: I learned that syntax for the fread() function from the book of Herbert
Schidt - somebody who was in the ANSI/ISO comittee - i thought he should
know such things...
Whatever..

Thanks just again !


Herbert Schildt's name is mud around this newsgroup. I haven't read any of
his stuff myself. But his poor reputation seems to be unanimous.

If you don't have anything else, get _The C Programming Language_, Second
Edition, by Kernighan and Ritchie.

You may also want to read the faq for this newsgroup.

http://www.eskimo.com/~scs/C-faq/top.html

Good luck

Mac
--

Nov 14 '05 #7
Alain Lafon wrote:
.... snip ...
Btw: I learned that syntax for the fread() function from the book
of Herbert Schidt - somebody who was in the ANSI/ISO comittee - i
thought he should know such things...


I believe he paid a fee to be on the committee, and never showed
up. He is a general laughing stock in the C world - his books,
while readable, are invariably loaded with mistakes. They are
generally referred to as BullSchildt, and recommended for lighting
fires.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #8
"Mac" <fo*@bar.net> writes:
[...]
Herbert Schildt's name is mud around this newsgroup. I haven't read any of
his stuff myself. But his poor reputation seems to be unanimous.


Indeed. He's the author of _The Annotated ANSI C Standard_, a book
containing the ISO C90 standard on the left-hand pages and his
commentary on the right-hand pages. The commentary is largely
nonsense. It's ably reviewed by Clive Feather at
<http://www.lysator.liu.se/c/schildt.html>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #9
Too bad i bought his book for just too much money then...

Thanks anyone for help !

Maybe if you're interested in what you helped building, take a look at
https://sourceforge.net/projects/pcif/

Thanks a lot again !

cya ^^
Nov 14 '05 #10
Alain Lafon <pr***@gmx.at> wrote:
Btw: I learned that syntax for the fread() function from the book of Herbert
Schidt - somebody who was in the ANSI/ISO comittee - i thought he should
know such things...
Whatever..

Oaaahhhhh .... horror .... dum ... dum dum dum dum dum dadaaaa ...
The name of H. Schildt has be brought to clc .... call ghost busters,
hurry up ....

Here some pointer on why one should avoid Mr. Schildts books:

http://www.lysator.liu.se/c/schildt.html

Do yourself a favor and put this book far away for the time being,
return to it just for fun when you have learned to program in C.
--
Z (Zo**********@daimlerchrysler.com)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 14 '05 #11

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

Similar topics

8
by: Brady | last post by:
Hi, I'm having a problem reading and writing to a file. What I'm trying to do is read a file, modify the portion of the file that I just read, and then write the modified data back to the same...
22
by: Rajshekhar | last post by:
Hi , i am writing a simple prgm to read a .txt file then store the contents into the array... program as follows: -------------------------- #include<stdio.h> int main() { FILE *fp1;
4
by: rendl42 | last post by:
Hey all, I'm a relative newbie to PHP, and am getting some strange results with the fread() function with PHP (win32) 5.1.2. Here is some code: <?php echo $_SERVER."<br>"; echo $_SERVER;...
13
by: 010 010 | last post by:
I found this very odd and maybe someone can explain it to me. I was using fread to scan through a binary file and pull bytes out. In the middle of a while loop, for no reason that i could...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
5
by: David Mathog | last post by:
When reading a binary input stream with fread() one can read N bytes in two ways : count=fread(buffer,1,N,fin); /* N bytes at a time */ or count=fread(buffer,N,1,fin); /* 1 buffer at a...
3
by: Harry | last post by:
#include<stdio.h> #include<stdlib.h> #include<malloc.h> void scramble(void); struct bmp_header { short int sig; int size_bmp;
24
by: kindrain | last post by:
the code checks whether a.txt has exact the same lines, then write different lines into b.txt Here it is: (before that ,you should creat a.txt) ---------------------- #include<stdio.h>...
9
by: xiao | last post by:
It always dumped when I tried to run it... But it compiles OK. What I want to do is to do a test: Read information from a .dat file and then write it to another file. The original DAT file is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.