繁体   English   中英

将一个文件复制到C中的另一个错误

[英]Copy one file into another error in c

我有这些多个错误和警告,并且我已经尝试了几乎所有内容,但无法解决。 非常感谢您的帮助! 这是我的代码:

 #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 *'

非常感谢您的帮助!

在您的代码dst_file是一个char [20] ,用于fopen ,获得一个FILE * ,该FILE *存储在dst_p

代替fputc(c, dst_file)尝试fputc(c, dst_p)

您需要认识到,当获取文件名时,您正在读取的输入行包括(取决于操作系统)换行符,回车符或两者。 因此,您需要一种方便的方法来删除这些无关的字符。

我从perl等人那里借来一个想法,我喜欢chomp(),它从行尾删除了'\\ n'和'\\ r'。

您还需要注意文件名与文件句柄,文件名上的fputc而不是文件句柄。

char [20]对于文件名来说很小,尝试200+; 您可能会发现“ w +”将在不存在时创建输出文件; 并使用sizeof(variable)表示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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM