strdup + fread = fail?

L

loudking

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

Richard Heathfield

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

Richard Tobin

loudking said:
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
 
G

Gunvant Patil

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

CBFalconer

Gunvant said:
.... 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
 
G

Gunvant Patil

Gunvant Patil wrote:

... snip ...


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

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

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

Similar Threads

URGENT 1
fread/fwrite 2 18
Help with EXT3 Filesystem work 1
error 28
file bug 28
struct and pointer question 33
fread returns more than i want 9
regarding lseek and fread 2

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top