用了几种方法,都是拷文本文件没问题,但拷二进制文件都不完整
代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, char** argv)
{
if (3 != argc)
{
printf("Usage: %s source_file dest_file.\n", argv[0]);
exit(1);
}
FILE *fsrc, *fdst;
if ((fsrc = fopen(argv[1], "rb")) == NULL)
{
fprintf(stderr, "Failed to open %s.\n", argv[1]);
exit(1);
}
if ((fdst = fopen(argv[2], "wb")) == NULL)
{
fprintf(stderr, "Failed to open %s.\n", argv[2]);
exit(1);
}
// char ch;
// while ((ch = getc(fsrc)) != EOF)
// {
// putc(ch, fdst);
// }
// char buf[8] = {0};
// while (fgets(buf, 8, fsrc) != NULL)
// {
// fputs(buf, fdst);
// }
char *ret = NULL;
char buf[20]="";
while(1){
ret = fgets(buf,6,fsrc);
if(ret == NULL){
perror("fgets");
break;
}
else{
if(fputs(buf,fdst) < 0) {
perror("fputs");
break;
}
}
}
fclose(fsrc);
fclose(fdst);
}
代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, char** argv)
{
if (3 != argc)
{
printf("Usage: %s source_file dest_file.\n", argv[0]);
exit(1);
}
FILE *fsrc, *fdst;
if ((fsrc = fopen(argv[1], "rb")) == NULL)
{
fprintf(stderr, "Failed to open %s.\n", argv[1]);
exit(1);
}
if ((fdst = fopen(argv[2], "wb")) == NULL)
{
fprintf(stderr, "Failed to open %s.\n", argv[2]);
exit(1);
}
// char ch;
// while ((ch = getc(fsrc)) != EOF)
// {
// putc(ch, fdst);
// }
// char buf[8] = {0};
// while (fgets(buf, 8, fsrc) != NULL)
// {
// fputs(buf, fdst);
// }
char *ret = NULL;
char buf[20]="";
while(1){
ret = fgets(buf,6,fsrc);
if(ret == NULL){
perror("fgets");
break;
}
else{
if(fputs(buf,fdst) < 0) {
perror("fputs");
break;
}
}
}
fclose(fsrc);
fclose(fdst);
}