Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with fread()

Alain Lafon
Guest
 
Posts: n/a
#1: Nov 14 '05
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.


Jack Klein
Guest
 
Posts: n/a
#2: Nov 14 '05

re: Problem with fread()


On Thu, 29 Jan 2004 05:35:36 +0100, "Alain Lafon" <preek@gmx.at> wrote
in comp.lang.c:
[color=blue]
> 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.[/color]

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
Alain Lafon
Guest
 
Posts: n/a
#3: Nov 14 '05

re: Problem with fread()


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.


nrk
Guest
 
Posts: n/a
#4: Nov 14 '05

re: Problem with fread()


Alain Lafon wrote:
[color=blue]
> 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;[/color]

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.
[color=blue]
> -some other variables-
> -snip-
> fscanf(fp_q, "%i", &x); //gets the size of the string which is to be
> read by fread()[/color]

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 */
[color=blue]
> fread(&file_qn, x, 1, fp_q); //this is exactly where the program[/color]
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 */
[color=blue]
> simply
> closes with no further messages.[/color]

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.
[color=blue]
> -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.[/color]

--
Remove devnull for email
Alain Lafon
Guest
 
Posts: n/a
#5: Nov 14 '05

re: Problem with fread()


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 !


Vijay Kumar R Zanvar
Guest
 
Posts: n/a
#6: Nov 14 '05

re: Problem with fread()


[..][color=blue]
> void abhaengen(FILE *fp_q, int key)
> {
> int i;[/color]

int x;
[color=blue]
> 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()[/color]

"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 );
[color=blue]
> fread(&file_qn, x, 1, fp_q); //this is exactly where the program simply
> closes with no further messages.
> -snip-[/color]

And, do not forget to free the allocated memory.
[..]


--
Vijay Kumar R Zanvar


Mac
Guest
 
Posts: n/a
#7: Nov 14 '05

re: Problem with fread()


On Thu, 29 Jan 2004 06:33:06 +0100, Alain Lafon wrote:
[color=blue]
> 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 ![/color]

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

CBFalconer
Guest
 
Posts: n/a
#8: Nov 14 '05

re: Problem with fread()


Alain Lafon wrote:[color=blue]
>[/color]
.... snip ...[color=blue]
>
> 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...[/color]

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 (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


Keith Thompson
Guest
 
Posts: n/a
#9: Nov 14 '05

re: Problem with fread()


"Mac" <foo@bar.net> writes:
[...][color=blue]
> 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.[/color]

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) kst-u@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"
Alain Lafon
Guest
 
Posts: n/a
#10: Nov 14 '05

re: Problem with fread()


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


Zoran Cutura
Guest
 
Posts: n/a
#11: Nov 14 '05

re: Problem with fread()


Alain Lafon <preek@gmx.at> wrote:[color=blue]
> 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..[/color]


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 (Zoran.Cutura@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
Closed Thread


Similar C / C++ bytes