C 为什么此函数不复制输入文件?
我刚刚开始研究文件I/O,并尝试构建一个函数,该函数只需将文件复制到目标 该程序进行编译,但会创建一个空文件,并且不会复制任何内容。有什么建议吗C 为什么此函数不复制输入文件?,c,file-io,C,File Io,我刚刚开始研究文件I/O,并尝试构建一个函数,该函数只需将文件复制到目标 该程序进行编译,但会创建一个空文件,并且不会复制任何内容。有什么建议吗 #include <stdio.h> int copy_file(char FileSource[], char FileDestination[]) { char content; FILE *inputf = fopen(FileSource, "r"); FILE *outputf = fopen(File
#include <stdio.h>
int copy_file(char FileSource[], char FileDestination[]) {
char content;
FILE *inputf = fopen(FileSource, "r");
FILE *outputf = fopen(FileDestination, "w");
if (inputf == NULL)
;
printf("Error: File could not be read \n");
return;
while ((content = getc(inputf)) != EOF) putc(content, inputf);
fclose(outputf);
fclose(inputf);
printf("Your file was successfully copied");
return 0;
}
int main() {
char inputname[100];
char outputname[100];
printf("Please enter input file name: \n");
scanf("%s", &inputname);
printf("Please write output file name: \n");
scanf("%s", &outputname);
copy_file(inputname, outputname);
return 0;
}
#包括
int copy_文件(char FileSource[],char FileDestination[]){
煤焦含量;
FILE*inputf=fopen(FileSource,“r”);
FILE*outputf=fopen(FileDestination,“w”);
if(inputf==NULL)
;
printf(“错误:无法读取文件\n”);
返回;
而((content=getc(inputf))!=EOF)putc(content,inputf);
fclose(outputf);
fclose(inputf);
printf(“您的文件已成功复制”);
返回0;
}
int main(){
字符输入名[100];
字符输出名[100];
printf(“请输入输入文件名:\n”);
scanf(“%s”、&inputname);
printf(“请写入输出文件名:\n”);
scanf(“%s”、&outputname);
复制_文件(inputname、outputname);
返回0;
}
您的线路:
putc(content, inputf);
需要换成
putc(content, outputf);
您提到的代码中几乎没有bug。下面这两个句子
scanf("%s", &inputname);
scanf("%s", &outputname);
是错误的,因为inputname
和outputname
是字符数组和数组名称本身的地址,因此无需将&inputname
提供给scanf()
。例如
scanf("%s",inputname);
scanf("%s",outputname);
也如果
语句没有达到您预期的正确目的,则在末尾添加code>
这个
应该是
if(inputf == NULL){
/*error handling */
}
正如other所指出的,getc()
返回int
而不是char
。从getc()的手册页
intgetc(文件*流)代码>
还有这个
putc(content, inputf);
改为
putc(content, outputf); /* write the data into outputf */
此代码有很多问题:
if(inputf == NULL);
printf("Error: File could not be read \n");
return;
它相当于
if(inputf == NULL)
{
;
}
printf("Error: File could not be read \n");
return;
你有一个迷路的
终止您的if
语句,而空格对C根本不重要
因此,如果您的语句不执行任何操作,您的代码将始终发出“错误:无法读取文件”消息,并在不执行任何其他操作的情况下返回
您可能想要的:
if(inputf == NULL)
{
printf("Error: File could not be read \n");
return;
}
这是一个很好的例子,说明了为什么很多C程序员总是在if
语句之后使用大括号始终代码中存在多个问题:
#include <stdio.h>
#include <string.h>
#include <sys/sendfile.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
if (argc < 3)
{
printf("Usage: %s <srcfile> <dst_file>\n", argv[0]);
return 1;
}
char *src_file = argv[1];
char *dst_file = argv[2];
int src;
int dst;
ssize_t size;
struct stat stat_buf;
if ((src = open(src_file, O_RDONLY)) < 0)
{
printf("Can not open %s\n", src_file);
return -1;
}
if (fstat(src, &stat_buf) < 0)
{
printf("Can stat %s\n", src_file);
close(src);
return -2;
}
if ((dst = open(dst_file, O_CREAT|O_WRONLY, stat_buf.st_mode)) < 0)
{
printf("Can not open %s\n", dst_file);
return -1;
}
if ((size = sendfile(dst, src, NULL, stat_buf.st_size)) < 0)
{
printf("Fail to copy file, size: %ld\n", size);
}
else
{
printf("Success, size: %ld\n", size);
}
close(src);
close(dst);
return 0;
}
content
必须声明为int
:getc()
返回一个int
,其中包含从文件读取的字节值或文件末尾的特殊负值EOF
。将其存储到char
变量会丢失信息,从而导致EOF
测试不明确(如果char
有符号)或始终为false(如果char
默认无符号)
- 您应该将
outputf
传递到putc
- 如果
fopen
无法打开任何一个文件,您应该从copy_file
功能返回
- 您应该为文件名传递要读取的最大字符数
- 您应该检查
scanf()
的返回值,以避免在无效输入上出现未定义的行为
以下是更正的版本:
#include <stdio.h>
int copy_file(const char *FileSource, const char *FileDestination) {
int content;
FILE *inputf, *outputf;
if ((inputf = fopen(FileSource, "r")) == NULL) {
printf("Error: cannot open input file %s\n", FileSource);
return -1;
}
if ((outputf = fopen(FileDestination, "w")) == NULL) {
printf("Error: cannot open output file %s\n", FileDestination);
fclose(inputf);
return -1;
}
while ((content = getc(inputf)) != EOF)
putc(content, inputf);
fclose(outputf);
fclose(inputf);
printf("Your file was successfully copied");
return 0;
}
int main() {
char inputname[100];
char outputname[100];
printf("Please enter input file name: \n");
if (scanf("%99s", inputname) != 1)
return 1;
printf("Please write output file name: \n");
if (scanf("%99s", &outputname) != 1)
return 1;
copy_file(inputname, outputname);
return 0;
}
#包括
int copy_文件(常量字符*文件源,常量字符*文件目标){
智力内容;
文件*inputf,*outputf;
if((inputf=fopen(FileSource,“r”))==NULL){
printf(“错误:无法打开输入文件%s\n”,FileSource);
返回-1;
}
if((outputf=fopen(FileDestination,“w”))==NULL){
printf(“错误:无法打开输出文件%s\n”,FileDestination);
fclose(inputf);
返回-1;
}
while((content=getc(inputf))!=EOF)
putc(内容、输入);
fclose(outputf);
fclose(inputf);
printf(“您的文件已成功复制”);
返回0;
}
int main(){
字符输入名[100];
字符输出名[100];
printf(“请输入输入文件名:\n”);
如果(scanf(“%99s”,inputname)!=1)
返回1;
printf(“请写入输出文件名:\n”);
如果(scanf(“%99s”,&outputname)!=1)
返回1;
复制_文件(inputname、outputname);
返回0;
}
使用sendfile()复制文件更简单有效。您可以通过man sendfile查看有关sendfile()的更多详细信息
#include <stdio.h>
#include <string.h>
#include <sys/sendfile.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
if (argc < 3)
{
printf("Usage: %s <srcfile> <dst_file>\n", argv[0]);
return 1;
}
char *src_file = argv[1];
char *dst_file = argv[2];
int src;
int dst;
ssize_t size;
struct stat stat_buf;
if ((src = open(src_file, O_RDONLY)) < 0)
{
printf("Can not open %s\n", src_file);
return -1;
}
if (fstat(src, &stat_buf) < 0)
{
printf("Can stat %s\n", src_file);
close(src);
return -2;
}
if ((dst = open(dst_file, O_CREAT|O_WRONLY, stat_buf.st_mode)) < 0)
{
printf("Can not open %s\n", dst_file);
return -1;
}
if ((size = sendfile(dst, src, NULL, stat_buf.st_size)) < 0)
{
printf("Fail to copy file, size: %ld\n", size);
}
else
{
printf("Success, size: %ld\n", size);
}
close(src);
close(dst);
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
如果(argc<3)
{
printf(“用法:%s\n”,argv[0]);
返回1;
}
char*src_file=argv[1];
char*dst_file=argv[2];
int-src;
int dst;
大小;
结构统计;
如果((src=open(src_文件,O_RDONLY))<0)
{
printf(“无法打开%s\n”,src\u文件);
返回-1;
}
如果(fstat(src和stat_buf)<0)
{
printf(“Can stat%s\n”,src\u文件);
关闭(src);
返回-2;
}
如果((dst=open(dst_文件,O_创建| O_WRONLY,stat_buf.st_模式))<0)
{
printf(“无法打开%s\n”,dst_文件);
返回-1;
}
if((size=sendfile(dst,src,NULL,stat_buf.st_size))<0)
{
printf(“复制文件失败,大小:%ld\n”,大小);
}
其他的
{
printf(“成功,大小:%ld\n”,大小);
}
关闭(src);
关闭(dst);
返回0;
}
OT:getc
返回一个int
-不是char
谁能告诉我fgets和get以及fputs和put之间的区别吗?@franx get/put使用标准输入/输出流。在fgets/fput中,您必须指明i/o流。谢谢。它确实奏效了。然而,当我尝试使用fprintf和fscanf时,它不起作用。此外,有人能告诉我fgets和get以及fputs和put之间的区别吗?我认为这应该是公认的答案,因为它解决了OP代码中的关键问题。