我有这些多重错误和警告,我已经尝试过几乎所有事情并且无法弄明白。非常感谢您的帮助!这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
/* Create Usable Variables */
FILE *src_p, *dst_p;
char src_file[20], dst_file[20];
char c;
/* Retrieve Source File Name From User */
printf("Enter Source File Name:\n");
fgets(src_file, 19, stdin);
/* Attempt Opening Source File For Reading */
if( (src_p = fopen(src_file, "r")) == NULL )
{
printf("ERROR: Source File Failed To Open...\n");
return(-1);
}
/* Retrieve Destination File Name From User */
printf("Enter Destination File Name:\n");
fgets(dst_file, 19, stdin);
/* Attempt Opening Destination File For Writing */
if( (dst_p = fopen(dst_file, "w")) == NULL )
{
fclose(src_p);
printf("ERROR: Destination File Failed To Open...\n");
return(-2);
}
/* Copy Source File Contents Into Destination File */
while( (c = fgetc(src_p)) != EOF )
fputc(c, dst_file);
/* Close Files On Success */
fclose(src_p);
fclose(dst_p);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
/* Create Usable Variables */
FILE *src_p, *dst_p;
char src_file[20], dst_file[20];
char c;
/* Retrieve Source File Name From User */
printf("Enter Source File Name:\n");
fgets(src_file, 19, stdin);
/* Attempt Opening Source File For Reading */
if( (src_p = fopen(src_file, "r")) == NULL )
{
printf("ERROR: Source File Failed To Open...\n");
return(-1);
}
/* Retrieve Destination File Name From User */
printf("Enter Destination File Name:\n");
fgets(dst_file, 19, stdin);
/* Attempt Opening Destination File For Writing */
if( (dst_p = fopen(dst_file, "w")) == NULL )
{
fclose(src_p);
printf("ERROR: Destination File Failed To Open...\n");
return(-2);
}
/* Copy Source File Contents Into Destination File */
while( (c = fgetc(src_p)) != EOF )
fputc(c, dst_file);
/* Close Files On Success */
fclose(src_p);
fclose(dst_p);
return 0;
}
我尝试编译时的错误是:
copyfile.c:在函数'main'中: copyfile.c:44:3:警告:从不兼容的指针类型[默认启用]传递'fputc'的参数2 在copyfile.c中包含的文件中:1:0: /usr/include/stdio.h:573:12:注意:预期'struct FILE *'但参数类型为'char *'
非常感谢您的帮助!
答案 0 :(得分:1)
在您的代码dst_file
中char [20]
是fopen
,用于FILE *
,获取您dst_p
中存储的fputc(c, dst_file)
。
而不是fputc(c, dst_p)
尝试{{1}}。
答案 1 :(得分:0)
您需要意识到,当您输入文件名时,您正在读取包含(取决于操作系统)换行符,回车符或两者的输入行。因此,您需要一种方便的方法来删除这些无关紧要的角色。
借用perl等人的想法,我喜欢chomp(),它从行的末尾删除'\ n'和'\ r'。
您还需要注意文件名与文件句柄,文件名上的fputc而不是文件句柄。
char [20]对于文件名来说相当小,尝试200+;并且您可能会发现“w +”将在不存在时创建输出文件;并将sizeof(变量)用于fgets大小,而不是硬编码大小。
这有效,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* chomp(char* p)
{
int len;
if(!p) return(p);
if( (len=strlen(p))<=0 ) return(p);
if( p[len-1] == '\n' ) { p[--len] = '\0'; }
if( p[len-1] == '\r' ) { p[--len] = '\0'; }
return(p);
}
int main()
{
/* Create Usable Variables */
FILE *src_fh, *dst_fh;
char src_fn[256+1], dst_fn[256+1];
/* Retrieve Source File Name From User */
printf("Enter Source File Name:\n");
fgets(src_fn, sizeof(src_fn), stdin); chomp(src_fn);
/* Attempt Opening Source File For Reading */
if( (src_fh = fopen(src_fn, "r")) == NULL )
{
printf("ERROR: Source File %s Failed To Open...\n",src_fn);
return(-1);
}
/* Retrieve Destination File Name From User */
printf("Enter Destination File Name:\n");
fgets(dst_fn, sizeof(dst_fn), stdin); chomp(dst_fn);
/* Attempt Opening Destination File For Writing */
if( (dst_fh = fopen(dst_fn, "w+")) == NULL )
{
fclose(src_fh);
printf("ERROR: Destination File %s Failed To Open...\n",dst_fn);
return(-2);
}
int ch;
/* Copy Source File Contents Into Destination File */
while( (ch = fgetc(src_fh)) != EOF )
{
fputc(ch, dst_fh);
}
/* Close Files On Success */
fclose(src_fh);
fclose(dst_fh);
return 0;
}