468,771 Members | 1,908 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

strdup + fread = fail?

Dear all,

I encountered a problem with fread and fwrite. If I am going to copy a
file using the same string, it will succeed

char *file_content;
struct stat buf;
FILE *fp, *new_fp;

fread(file_content, sizeof(char), buf.st_size, fp);
fwrite(file_content, sizeof(char), buf.st_size, new_fp);

If I duplicate that string, the new file will be different from the
original one.

char *bak;
fread(file_content, sizeof(char), buf.st_size, fp);
bak = strdup(file_content);
fwrite(bak, sizeof(char), buf.st_size, new_fp);

I checked the return value of both fread and fwrite, the result is
identical. I even used strcmp to compare these two strings, but they
are identical, too. so I don't know why this happens ...

BTW: fp and new_fp points to pdf files.

Apr 17 '07 #1
5 2143
loudking said:
Dear all,

I encountered a problem with fread and fwrite. If I am going to copy a
file using the same string, it will succeed

char *file_content;
struct stat buf;
FILE *fp, *new_fp;

fread(file_content, sizeof(char), buf.st_size, fp);
fwrite(file_content, sizeof(char), buf.st_size, new_fp);

If I duplicate that string, the new file will be different from the
original one.

char *bak;
fread(file_content, sizeof(char), buf.st_size, fp);
bak = strdup(file_content);
fwrite(bak, sizeof(char), buf.st_size, new_fp);

I checked the return value of both fread and fwrite, the result is
identical. I even used strcmp to compare these two strings, but they
are identical, too. so I don't know why this happens ...
Check that strlen(bak) == buf.st_size - 1 and that
file_content[buf.st_size - 1] is 0. If both these are true, you have
cause for concern. If either is false, you have cause to pause for
thought, after which you should be able to deduce what is going on.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 17 '07 #2
In article <11**********************@l77g2000hsb.googlegroups .com>,
loudking <lo******@gmail.comwrote:
>fread(file_content, sizeof(char), buf.st_size, fp);
bak = strdup(file_content);
fread() doesn't nul-terminate the data it reads, so you shouldn't call
strdup() on it. If the data contains nul characters, you will only
copy up to the first one; if it doesn't you will run off the end.

For binary data you should use the known length (buf.st_size, assuming
the fread() succeeds, which you should check) to allocate space for
the copy, and then use memcpy().

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Apr 17 '07 #3
On Apr 17, 11:32 am, loudking <loudk...@gmail.comwrote:
Dear all,

I encountered a problem with fread and fwrite. If I am going to copy a
file using the same string, it will succeed

char *file_content;
struct stat buf;
FILE *fp, *new_fp;

fread(file_content, sizeof(char), buf.st_size, fp);
fwrite(file_content, sizeof(char), buf.st_size, new_fp);

If I duplicate that string, the new file will be different from the
original one.

char *bak;
fread(file_content, sizeof(char), buf.st_size, fp);
bak = strdup(file_content);
fwrite(bak, sizeof(char), buf.st_size, new_fp);

I checked the return value of both fread and fwrite, the result is
identical. I even used strcmp to compare these two strings, but they
are identical, too. so I don't know why this happens ...

BTW: fp and new_fp points to pdf files.
fread() doesn't guarantee to return a "C" string (terminated with NUL
char '\0') and strdup() is supposed to work on "C" strings only.
Better allocate enough space and your memcpy()

-Cheers,
Gunvant
~~~~~~~~
No trees were killed in the sending of this message. However a large
number of electrons were terribly inconvenienced.

Apr 17 '07 #4
Gunvant Patil wrote:
>
.... snip ...
>
fread() doesn't guarantee to return a "C" string (terminated with
NUL char '\0') and strdup() is supposed to work on "C" strings
only. Better allocate enough space and your memcpy()
strdup doesn't exist in standard 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

--
Posted via a free Usenet account from http://www.teranews.com

Apr 17 '07 #5
On Apr 17, 10:38 pm, CBFalconer <cbfalco...@yahoo.comwrote:
Gunvant Patil wrote:

... snip ...
fread() doesn't guarantee to return a "C" string (terminated with
NUL char '\0') and strdup() is supposed to work on "C" strings
only. Better allocate enough space and your memcpy()

strdup doesn't exist in standard C.
Yes, i do agree and i never said that it is part of C standards.
--
<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

--
Posted via a free Usenet account fromhttp://www.teranews.com
-Cheers,
Gunvant
~~~~~~~~
No trees were killed in the sending of this message. However a large
number of electrons were terribly inconvenienced.

Apr 18 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

39 posts views Thread by Allan Bruce | last post: by
6 posts views Thread by Stefan Schwärzler | last post: by
32 posts views Thread by Grumble | last post: by
13 posts views Thread by 010 010 | last post: by
5 posts views Thread by David Mathog | last post: by
53 posts views Thread by klaushuotari | last post: by
20 posts views Thread by ericunfuk | last post: by
20 posts views Thread by Michael Holm | last post: by
7 posts views Thread by Richie Kernagan | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.