473,782 Members | 2,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fread/fwrite

How to use fread/fwrite copy a file.
When reach file's end, fread return 0, I don't konw how many bytes
in buf.

May 17 '07 #1
30 14284
empriser wrote:
How to use fread/fwrite copy a file.
When reach file's end, fread return 0, I don't konw how many bytes
in buf.
None.

Don't you have documentation for these functions? Or man pages? Or
access to Google?

--
"Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

May 17 '07 #2
On 17 May 2007 00:52:29 -0700, empriser <xu********@gma il.comwrote:
>How to use fread/fwrite copy a file.
When reach file's end, fread return 0, I don't konw how many bytes
in buf.
It depends on how code the fread. If you specify 1 block of n
characters and there is less than n left in the file, then it will
return 0. However, if you specify n blocks of 1 character each, then
it will return the number of blocks read (which will be exactly the
same as the number of characters read).
Remove del for email
May 17 '07 #3
On May 17, 8:25 pm, Barry Schwarz <schwa...@doezl .netwrote:
On 17 May 2007 00:52:29 -0700, empriser <xueyunl...@gma il.comwrote:
How to use fread/fwrite copy a file.
When reach file's end, fread return 0, I don't konw how many bytes
in buf.

It depends on how code the fread. If you specify 1 block of n
characters and there is less than n left in the file, then it will
return 0. However, if you specify n blocks of 1 character each, then
it will return the number of blocks read (which will be exactly the
same as the number of characters read).

Remove del for email
Yes my buffer size is n (n 1).
How do I know there are how many bytes in buffer, after last call
fread as if return 0.

Jul 5 '07 #4
empriser wrote:
On May 17, 8:25 pm, Barry Schwarz <schwa...@doezl .netwrote:
>On 17 May 2007 00:52:29 -0700, empriser <xueyunl...@gma il.comwrote:
>>How to use fread/fwrite copy a file.
When reach file's end, fread return 0, I don't konw how many bytes
in buf.
It depends on how code the fread. If you specify 1 block of n
characters and there is less than n left in the file, then it will
return 0. However, if you specify n blocks of 1 character each, then
it will return the number of blocks read (which will be exactly the
same as the number of characters read).

Remove del for email

Yes my buffer size is n (n 1).
How do I know there are how many bytes in buffer, after last call
fread as if return 0.
fread returns the number of elements that were read and
stored in the buffer. If fread returns 10, it successfully
read and stored 10 elements. If fread returns 0, it read
and stored ... <wait for it... no elements.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jul 5 '07 #5
On Thu, 05 Jul 2007 05:00:26 -0000, empriser <xu********@gma il.com>
wrote:
>On May 17, 8:25 pm, Barry Schwarz <schwa...@doezl .netwrote:
>On 17 May 2007 00:52:29 -0700, empriser <xueyunl...@gma il.comwrote:
>How to use fread/fwrite copy a file.
When reach file's end, fread return 0, I don't konw how many bytes
in buf.

It depends on how code the fread. If you specify 1 block of n
characters and there is less than n left in the file, then it will
return 0. However, if you specify n blocks of 1 character each, then
it will return the number of blocks read (which will be exactly the
same as the number of characters read).

Remove del for email

Yes my buffer size is n (n 1).
How do I know there are how many bytes in buffer, after last call
fread as if return 0.
If you don't show your code, everything is just a guess. As long as
we are guessing, I guess that the number of bytes read is equal to the
square root of the address of fread (when cast to an unsigned short).
Remove del for email
Jul 6 '07 #6
Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
empriser wrote:
...
Yes my buffer size is n (n 1).
How do I know there are how many bytes in buffer,
after last call fread as if return 0.

fread returns the number of elements that were
read and stored in the buffer. If fread returns 10,
it successfully read and stored 10 elements. If
fread returns 0, it read and stored ... <wait for
it... no elements.
In other words, code as...

static char buf[1024];
size_t r = fread(buf, 1, sizeof buf, fp);

....rather than...

static char buf[1024];
size_t r = fread(buf, sizeof buf, 1, fp);

Assuming no read errors, if there are 123 characters
left in the stream, the former will return 0 into r,
whereas the latter will return 123. Both calls will
read the same number of characters though.

--
Peter

Jul 6 '07 #7
OK I write a easy program to show it.
fread( buf, sizeof(buf), 1, rf );
fwrite( buf, sizeof(buf), 1, wf );

If I don't change to fread( buf, 1, sizeof(buf), rf ); how to copy a
file.

int main( int argc, char **argv )
{
int i;
char buf[1024];
FILE *rf, *wf;

rf = fopen( argv[1], "r" );
wf = fopen( argv[2], "w" );

i = fread( buf, sizeof(buf), 1, rf );
while( i 0 ){
fwrite( buf, sizeof(buf), 1, wf );
i = fread( buf, sizeof(buf), 1, rf );
}

fclose( rf );
fclose( wf );
return 0;
}

Jul 7 '07 #8
On Sat, 07 Jul 2007 03:04:46 -0000, empriser <xu********@gma il.com>
wrote:
>OK I write a easy program to show it.
fread( buf, sizeof(buf), 1, rf );
fwrite( buf, sizeof(buf), 1, wf );

If I don't change to fread( buf, 1, sizeof(buf), rf ); how to copy a
file.
By changing fread to fgetc and fwrite to fputc.

What is with the spate of "I've got this broken code I don't want to
change. How do I make it work?" messages. The only way to fix broken
code is to change it. It you don't change it, it remains broken.

If I insist on putting water in my gas tank, how do I get my car to
run?
>
int main( int argc, char **argv )
{
int i;
char buf[1024];
FILE *rf, *wf;

rf = fopen( argv[1], "r" );
wf = fopen( argv[2], "w" );

i = fread( buf, sizeof(buf), 1, rf );
while( i 0 ){
fwrite( buf, sizeof(buf), 1, wf );
i = fread( buf, sizeof(buf), 1, rf );
}

fclose( rf );
fclose( wf );
return 0;
}

Remove del for email
Jul 7 '07 #9
empriser wrote:
OK I write a easy program to show it.
fread( buf, sizeof(buf), 1, rf );
fwrite( buf, sizeof(buf), 1, wf );

If I don't change to fread( buf, 1, sizeof(buf), rf ); how to copy a
file.

int main( int argc, char **argv )
{
int i;
char buf[1024];
FILE *rf, *wf;

rf = fopen( argv[1], "r" );
wf = fopen( argv[2], "w" );

i = fread( buf, sizeof(buf), 1, rf );
while( i 0 ){
fwrite( buf, sizeof(buf), 1, wf );
i = fread( buf, sizeof(buf), 1, rf );
}

fclose( rf );
fclose( wf );
return 0;
}
Maybe this way..

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
FILE *in, *out;
char buf[100];
size_t size;

if ((in = fopen("cp.c", "rb")) == NULL)
puts("Can't open cp.c"), exit(EXIT_FAILU RE);

if ((out = fopen("cp.x", "wb")) == NULL)
puts("Can't make cp.x"), exit(EXIT_FAILU RE);

while ((size = fread(buf, 1, sizeof buf, in)) 0)
fwrite(buf, 1, size, out);

fclose(in);
fclose(out);
return 0;
}

Note that cp.c is the name of this source file.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jul 7 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
15360
by: Brady | last post by:
Hi, I'm having a problem reading and writing to a file. What I'm trying to do is read a file, modify the portion of the file that I just read, and then write the modified data back to the same location in the file. What is happening, is I can read the file, either in entirety or only part of it. And no matter what I try setting the position to, using fsetpos, it always gets set back to the very beginning of the file. I
2
15578
by: Luc Holland | last post by:
Hey, I'm working on a program that reads a binary file. It's opened with ==== if ((f1=fopen(argv,"rb"))==NULL) { fprintf(stderr,"Error opening %s for reading . . .\n",argv); exit(2); } ==== The structure of the file is:
4
9289
by: janssenssimon | last post by:
//de structure om de highscores in op de slagen typedef struct score{ char *naam; int veld; int score; struct score *volg; }HIGH; void toonhighscores(void)
2
6322
by: Richard Hsu | last post by:
// code #include "stdio.h" int status(FILE * f) { printf("ftell:%d, feof:%s\n", ftell(f), feof(f) != 0 ? "true" : "false"); } int case1() { FILE * f = fopen("c:\\blah", "wb+"); int i = 5;
8
17193
by: M. Åhman | last post by:
I'm reading "C: A Reference Manual" but still can't understand a very basic thing: is there any functional difference between fgetc/fputc and fread/fwrite (when reading/writing one unsigned char)? 1. Books and documentation on C tell me fread and fwrite is "binary". What does binary mean in this? Anything to do with opening a file in binary mode? 2. I'm also sure I've read somewhere on the net that fread and
2
3079
by: elisa | last post by:
Dear all, I have problems in writeing and reading a block of data (long array) with fread and fwrite. If I write and read an integer array, everything looks fine, but when I try long array, sth strange happen, the data read from a file is not the same the data I supposed to write. Since I can't view the binary data file, I don't know what's wrong. Here is my code, please help me, Thanks a lot! Elisa #include <stdio.h> #define...
1
5576
by: rohit deshpande | last post by:
i am writing a program in c,c++....... I want to read one file and write its contents using fread and fwrite functions. the program i hv written has no errors. but output file is not same as that of read file. main() of program is like this........ void main() { FILE *readfile,*writefile; unsigned long ml,mr; char ch; int noc=0,blen=0;
5
2372
by: loudking | last post by:
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);
4
3776
by: Highlander2nd | last post by:
Hello there. I'm Andrew Lucas, I'm a programmer for Half-Life. I've been working on stencil shadows lately, and I've been having problems saving mesh data for my models. When I store mesh data, I have to store data for each submodel and their respective triangle data, wich are the vertex and neighbor(triangles sharing a common edge) indexes. Now, I'm storing this data in these struct's: struct Face { Face() {} Face(GLushort v0,...
0
9641
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
10146
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
10080
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
8968
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...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
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
5378
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...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.