2013-10-02 47 views
0

我有這些多個錯誤和警告,並且我已經嘗試了幾乎所有的東西,但無法弄清楚。非常感謝您的幫助!這是我的代碼:將一個文件複製到另一個錯誤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:在函數 '主': copyfile.c:44:3:警告:從不兼容指針類型[默認情況下啓用]傳遞'fputc'的參數2 從copyfile.c包含的文件中:1:0: /usr/include/stdio.h:573:12:note:expected'struct FILE *'但是參數的類型是'char *'

您的幫助非常讚賞ED!

回答

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; 
} 
1

在您的代碼dst_filechar [20],您使用fopen,獲得FILE *,您存儲在dst_p

而不是fputc(c, dst_file)嘗試fputc(c, dst_p)

+0

LOL!男人......這樣一個簡單的錯誤,如此頭疼。謝謝。我會在3分鐘內接受你的回答。 –

+2

將來,這些簡單的錯誤可以通過仔細閱讀由編譯器報告的錯誤**輕鬆解決。嘗試從gcc再次讀取錯誤消息,看看你是否理解了@cnicutar給你的信息。你會從中學到很多 – Pankrates

+0

是的,謝謝。是否有更好的C調試器(當然不使用IDE)? –

相關問題