So I want to reada file's contents and write it to another one, overwriting it so the two have identical contents. And I would like to use a buffer so I'm trying to do this with fread() and fwrite():
Code:
#include <stdio.h>
#include <stdlib.h>
void exit_with_msg(const char *s) {
(void) fputs(s, stderr);
exit(EXIT_FAILURE);
}
int copy(FILE *src, FILE *dst) {
if (src == (FILE *)0 || dst == (FILE *)0) {
return -1;
}
unsigned char buf[512];
size_t n;
while ((n = fread(buf, 512, 1, src)) > 0) {
if (fwrite(buf, 512, 1, dst) != n) {
return -2;
}
}
if (ferror(src) != 0) {
return -1;
}
return 0;
}
int main(void) {
FILE *src = fopen("srcfile", "rb");
if (src == (FILE *)0) {
exit_with_msg("Couldn't open source file\n");
}
FILE *dst = fopen("dstfile", "wb");
if (dst == (FILE *)0) {
(void) fclose(src);
exit_with_msg("Couldn't open destination file\n");
}
int status = copy(src, dst);
if (status != 0) {
(void) fclose(src);
(void) fclose(dst);
char *s = "Couldn't write file contents.\n";
if (status == -1) {
s = "Couldn't read file contents.\n";
}
exit_with_msg(s);
}
(void) fclose(src);
(void) fclose(dst);
return 0;
}
But this doesn't copy the whole contents. For example if srcfile had this:
Code:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam placerat at
sapien sit amet efficitur. Phasellus id sem vel arcu laoreet malesuada. Quisque
elementum malesuada massa, ac convallis ante fringilla quis. Proin elementum
metus ac rutrum pharetra. Fusce et justo suscipit, pellentesque ipsum sed,
imperdiet lectus. In tortor elit, accumsan nec dolor eget, scelerisque fermentum
ipsum. Vestibulum sit amet imperdiet ipsum. Sed ornare, mauris at lobortis
porttitor, augue nisi vestibulum ipsum, non lobortis arcu neque posuere nibh.
Proin ac justo vel ipsum interdum mollis ac vitae tortor. Orci varius natoque
penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean
aliquam dignissim risus, in malesuada nibh auctor sed.
The contents of dstfile would be this:
Code:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam placerat at
sapien sit amet efficitur. Phasellus id sem vel arcu laoreet malesuada. Quisque
elementum malesuada massa, ac convallis ante fringilla quis. Proin elementum
metus ac rutrum pharetra. Fusce et justo suscipit, pellentesque ipsum sed,
imperdiet lectus. In tortor elit, accumsan nec dolor eget, scelerisque fermentum
ipsum. Vestibulum sit amet imperdiet ipsum. Sed ornare, mauris at lobortis
porttitor, augue nisi vestibulum ipsum, non lob
And I figured it's copying just the first 512 bytes because if fread() is passed 1 as the nitems aguments it returns 0 if the read is incomplete (incomplete meaning it didn't read exactly 512 bytes), so I passed 1 to the size argument and 512 to the nitems arguments:
Code:
#include <stdio.h>
#include <stdlib.h>
void exit_with_msg(const char *s) {
(void) fputs(s, stderr);
exit(EXIT_FAILURE);
}
int copy(FILE *src, FILE *dst) {
if (src == (FILE *)0 || dst == (FILE *)0) {
return -1;
}
unsigned char buf[512];
size_t n;
while ((n = fread(buf, 1, 512, src)) > 0) {
if (fwrite(buf, 1, 512, dst) != n) {
return -2;
}
}
if (ferror(src) != 0) {
return -1;
}
return 0;
}
int main(void) {
FILE *src = fopen("srcfile", "rb");
if (src == (FILE *)0) {
exit_with_msg("Couldn't open source file\n");
}
FILE *dst = fopen("dstfile", "wb");
if (dst == (FILE *)0) {
(void) fclose(src);
exit_with_msg("Couldn't open destination file\n");
}
int status = copy(src, dst);
if (status != 0) {
(void) fclose(src);
(void) fclose(dst);
char *s = "Couldn't write file contents.\n";
if (status == -1) {
s = "Couldn't read file contents.\n";
}
exit_with_msg(s);
}
(void) fclose(src);
(void) fclose(dst);
return 0;
}
But now the contents of dstfile is this:
Code:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam placerat at
sapien sit amet efficitur. Phasellus id sem vel arcu laoreet malesuada. Quisque
elementum malesuada massa, ac convallis ante fringilla quis. Proin elementum
metus ac rutrum pharetra. Fusce et justo suscipit, pellentesque ipsum sed,
imperdiet lectus. In tortor elit, accumsan nec dolor eget, scelerisque fermentum
ipsum. Vestibulum sit amet imperdiet ipsum. Sed ornare, mauris at lobortis
porttitor, augue nisi vestibulum ipsum, non lobortis arcu neque posuere nibh.
Proin ac justo vel ipsum interdum mollis ac vitae tortor. Orci varius natoque
penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean
aliquam dignissim risus, in malesuada nibh auctor sed.
s ac rutrum pharetra. Fusce et justo suscipit, pellentesque ipsum sed,
imperdiet lectus. In tortor elit, accumsan nec dolor eget, scelerisque fermentum
ipsum. Vestibulum sit amet imperdiet ipsum. Sed ornare, mauris at lobortis
porttitor, augue nisi vestibulum ipsum, non lob
So now it's reading the entire contents of srcfile, but instead of stopping when it reaches EOF, it goes back 425 bytes from the end of the file to read exactly another 512 byes???? Why is it doing that?? Shouldn't it stop reading when EOF is reached?