2011-12-10 55 views
0

下面的問題的主要描述,它發生的地方。但簡單地說,我不知道爲什麼我詢問後爲什麼會收到錯誤消息努力理解文件指針?

if (outf!=NULL){ 
    printf("Output file already exists, overwrite (y/n):"); 
    scanf("%c",yn); 
} 

其中outf是指向現有文件的文件指針。請在代碼的中途閱讀說明。

#include <stdio.h> 
#include <stdlib.h> 
#include <float.h> 
#include <string.h> 

int main() { 

/* Declare file pointer */ 
    FILE *inf; 
    FILE *outf; 

    int linenumber,linecounter=0,linepresent; 
    char filename[21]; 
    char detail[21]; 
    char linedetail[21]; 
    char outfilename[21]; 
    char letter,yn='y'; 
    int position; 

/*INPUT DETAILS Ask user for file name and line number*/ 

    printf("Please enter an input filename and a linenumber: "); 
//scan filename to char string and line number to int variable 
    scanf("%s %i",&filename,&linenumber); 

/*OUTPUT DETAILS Ask user for file name, letter & position, etc*/ 
    printf("Please enter an output filename, a letter and a position:");  
    scanf("%s %c %i",&outfilename,&letter,&position); 

/* Open file for reading */ 
    inf=fopen (filename,"r"); 
    outf=fopen(outfilename,"r"); 
/*check that file exists*/ 
    if (inf!=NULL) { 

直到這裏一切正常! 然後我試着找出outf文件是否已經存在。如果outf指向一個現有的文件,它會打印「輸出文件已經存在,覆蓋(y/n):」

但是一旦它打印出來,我就會打開錯誤窗口!這可能是一個非常新奇的錯誤 - 我仍然在學習C.如果沒有這樣的文件,程序正常完成並繞過if語句。

 if (outf!=NULL){ 
      printf("Output file already exists, overwrite (y/n):"); 
      scanf("%c",yn); 
     } 
     if (yn=='y'){ 
    /*keep reading to end of file*/ 
      while (feof(inf)==0) { 
       linecounter++; 
    /*read each line and store the line number in detail[WORDS GO HERE]*/ 
       fscanf (inf,"%s", &detail); 
    /*If we reach the line selected by the user*/ 
       if (linecounter==linenumber){ 
        strcpy(linedetail,detail); 
        linepresent=1; 
       } 
      } 
      if (linepresent==0) { 
       printf("File only contains %i lines",linecounter); 
      } 
     } else { 
      exit(1); 
     } 
    } else { 
     printf("Input file not found"); 
    } 

printf("%s",linedetail); 

/* close the file */ 

    fclose(inf); 
    fclose(outf); 

    return (0); 

} 
+1

不要忘記'&':'scanf(「%c」,&yn)' – pmg

+1

如果你發現有問題的行......你爲什麼不嘗試一個簡單的程序,只說'int main ){char yn ='y'; scanf(「%c」,yn);返回0; ''...當這個崩潰的職位是這樣嗎? : -/*(下次要考慮的事情是,當問題被減少到最小的情況下,最好的情況會產生錯誤,其他所有事情都會被帶出。)* – HostileFork

+0

謝謝,我確實需要改進我的提問技巧!爲每個人的耐心歡呼! – user1083734

回答

3

首先,已經提及的問題:你在閱讀模式下打開輸出文件。要打開它寫:

outf=fopen(outfilename,"w"); /* Note the "w". */ 

此外,scanf()的接受指針變量,而不是它們的值,因此,如果你寫scanf("%c", yn);,你給scanf函數的指針,這完全是無稽之談字符y。你需要這樣做:scanf("%c", &yn);

但是,即使您修復了這些問題,您的程序也不會達到您的預期。如果您試圖打開的文件不存在,fopen()將不會返回NULL,它會創建一個新文件。您的代碼將總是覆蓋輸出文件,如果它存在。返回NULL只有當fopen無法打開/創建文件(例如,您沒有權限做),你應該處理這樣的:

outf=fopen(outfilename, "w"); 
if(outf == NULL) { 
    perror("Failed to open output file: "); 
    fclose(inf); /* Don't leave files opened. It's bad form. */ 
    exit(1); 
} 
/* Open succeeded, do your stuff here. */ 

注意,不需要else塊在if之後,因爲exit()立即結束程序。

此外,沒有「指向文件的指針」這樣的東西。 FILE只是一個表示打開文件的結構。

0

http://www.cplusplus.com/reference/clibrary/cstdio/fopen/

你是開放與讀取的標記輸出文件。嘗試將其更改爲「w」。

outf=fopen(outfilename,"w"); 

雖然值得注意的是寫入用「w」打開的文件將會敲擊舊文件。使用「a」追加到文件。

+0

在我看來,OP打算首先通過以讀取模式打開文件來檢查文件是否存在。如果返回null,則假定文件不存在。這並不總是正確的,因爲缺少讀取現有文件的權限也可能返回null。 stat()庫函數也許是更好的選擇。 – Gowtham

+0

@Gowtham:我認爲OP認爲打開*輸出文件*將返回NULL,如果它不存在。除非他混淆了'if'條件中的變量...... – Staven

+0

@Staven您現在正在嘗試檢測輸出文件是否已經存在。如果確實存在,用戶將有選擇覆蓋或不覆蓋。如果它不存在,那麼我打算在「寫入」模式下創建它。我無法檢測它是否存在。我應該怎麼做呢? – user1083734

0

您應該將yn的地址傳遞給scanf函數。

scanf("%c", &yn);