Problem with fread()

A

Alain Lafon

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.
 
J

Jack Klein

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.
 
A

Alain Lafon

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.
 
N

nrk

Alain said:
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.
 
A

Alain Lafon

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 !
 
V

Vijay Kumar R Zanvar

[..]
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.
[..]
 
M

Mac

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
--
 
Z

Zoran Cutura

Alain Lafon said:
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.
 
C

CBFalconer

Alain said:
.... 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.
 
K

Keith Thompson

Mac said:
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>.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top