473,792 Members | 3,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_cont ent, sizeof(char), buf.st_size, fp);
fwrite(file_con tent, 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_cont ent, sizeof(char), buf.st_size, fp);
bak = strdup(file_con tent);
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 2372
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_cont ent, sizeof(char), buf.st_size, fp);
fwrite(file_con tent, 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_cont ent, sizeof(char), buf.st_size, fp);
bak = strdup(file_con tent);
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************ **********@l77g 2000hsb.googleg roups.com>,
loudking <lo******@gmail .comwrote:
>fread(file_con tent, sizeof(char), buf.st_size, fp);
bak = strdup(file_con tent);
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
--
"Considerat ion 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_cont ent, sizeof(char), buf.st_size, fp);
fwrite(file_con tent, 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_cont ent, sizeof(char), buf.st_size, fp);
bak = strdup(file_con tent);
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.securityfoc us.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...@yah oo.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.securityfoc us.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.co m
-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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
23656
by: Allan Bruce | last post by:
Hi there, I have a program written in c++ and I wish to use a similar function to strdup(). The reason I have problems is that the char array requires freeing to avoid a memory leak, but I get problems using delete; e.g. char *NewCharArray;
6
2947
by: Stefan Schwärzler | last post by:
Hi Ng, habe nicht besonders viel Erfahrung in C und C++, deshalb: möchte den befehl strdup in <string.h> verwenden. #include <string.h> attrib(char *name, char *val) : name(strdup(name)), val(strdup(val)), next(0) { CDEBUG(printf("attrib::attrib(%s, %s)\n", name, val)); }
32
6386
by: Grumble | last post by:
As far as I can tell, strdup() is neither in C89 nor in C99. Is that correct? <OT>Is it in POSIX perhaps?</OT>
13
3552
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 discern, fread all the sudden kept returning the same byte over and over as if it were no longer advancing in the file. I used a hex editor to determine the address of the last byte read in the file. CF was the last address, D0 was not ever read...
5
6357
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 time */ I would assume the latter form would be faster, or at least
53
651
by: klaushuotari | last post by:
Sorry to bother you, but I just have to. What about strdup()? It wasn't in standard C run-time library, yet many apps use it liberally as it was in there. I don't know if that particular function is included in C99. Not too difficult to code own version of it, but why it wasn't included in library in the first place? Is there some rationale behind it? There are peculiar and dangerous functions like strtok(), so why not strdup() which...
20
7551
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file frequently(forwards and backwards) and do fread() from that offset. Or better still, could anyone let me know some good ways to achieve what I need to do as above?Can I get hold of the file and being able to read in without using fread()? Using...
20
6131
by: Michael Holm | last post by:
Ever so often when I try to compile some open source code, I get a error, pbrtparse.y(205) : error C3861: 'strdup': identifier not found In the example, I'm trying to compile pbrt (http://www.pbrt.org/). And I have been searching all over the net for it, and it seems I'm the only one having a problem with strdup. What do I do wrong?
7
4642
by: Richie Kernagan | last post by:
Howdy Problem with the fread() call on MS Visual Studio 2005 running under Win 64. The parameters to fread size and count are both size_t ie 64 bits. Even so the call fails when the size arg is greater than 4GB (ie exceeds 32 bit unsigned value). Any suggestions.
0
9669
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9517
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10207
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10156
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9030
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5435
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.