2013-05-20 32 views
0

我想從文本文件中讀取數據並寫入其中,但每次執行我的代碼時,文本文件都沒有任何反應。通過「沒有任何反應」,我的意思是程序不會讀取我的輸入文件,也沒有數據導出到我的輸出文件中。有人能指出它爲什麼不起作用嗎?感謝您提前給予的任何幫助。這裏是我的代碼:C編程文件I/O

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

FILE *inptr, *outptr; 

int main() { 
    int a, b, c; 
    inptr = fopen("trianglein.txt","r"); //Initialization of pointer and opening of file trianglein.txt 
    outptr = fopen("triangleout.txt","w"); //Initialization of pointer and opening of file triangleout.txt 

    while((fscanf(inptr,"%d %d %d",&a, &b, &c))!= EOF){ 
     fprintf(outptr,"\n%2d %2d %2d\n",a,b,c); 
     if(a+b>c && b+c>a && c+a>b){ 
      fprintf(outptr, "This is a triangle.\n"); 
      if(a !=b && b !=c && a!=c){ 
       fprintf(outptr, "This is a scalene triangle.\n"); 
       if(a==b && a==c && c==b){ 
        fprintf(outptr, "This is an equilateral triangle.\n"); 
        if(a*a+b*b==c*c || b*b+c*c==a*a || a*a+c*c==b*b){ 
         fprintf(outptr, "This is a right trianlge.\n"); 
        } 
       } 
      } 
     } 
    } 

    return 0; 
} 

trianglein.txt內容:

10 12 15 
2 3 7 
3 4 5 
6 9 5 
6 6 6 
6 8 10 
7 7 9 
+4

它似乎爲我工作。就像我在運行時一樣,triangleout.txt包含了東西。你可以發佈你的trianglein.txt的內容嗎? (你也許想重新看看你的邏輯,因爲直角三角形不是等邊三角形。) – FDinoff

+0

順便說一句,你可能想用「繼續」來重構你的代碼 - 這會讓你避免大量的縮進 – thejh

+1

如果什麼都沒有發生那麼在工作目錄中沒有文件「trianglein.txt」。檢查返回fopen。把文件放在工作目錄下,如果是這種情況。同時檢查「trianglein.txt」的內容。使用調試器是個好主意btw。 – kotlomoy

回答

-2

編輯:正如icktoofay建議,這個答案是錯的。

您必須做fclose()fflush()才能將數據寫入文件。 前return 0;

fclose(inptr); 
fclose(outptr); 
+1

不可以。根據C99草案N1256§7.20.4.3¶4,所有流在程序終止時刷新。 – icktoofay

+0

添加這兩個代碼塊仍然不會改變任何東西。 ??? – Cranderberry

+0

@icktoofay哦,你說得對。我剛剛編譯了代碼。有用。無論如何,我不會刪除這個答案。 –

-1

插入這些代碼的權利嘗試把

fclose(inptr); 

fclose(outptr); 

在你的代碼的末尾。

+1

-1,這將無濟於事。 – thejh

+1

對不起,爲什麼不呢? – mecid

+0

+1。我相信它有幫助。 @thejh你能解釋一下你的想法嗎? –

3

多個問題。

首先,您需要通過對NULL進行測試來檢查inptr和outptr是否有效。

其次的fscanf可以返回EOF,0或> 0

如果輸入文件不包含有效的輸入。

也有問題,你可以得到3整數讀取成功,或2整數或1和a,b和c的值只是可選的設置。

如果輸入沒有發生轉換,則返回零值,在這種情況下,while循環將退出。

的同時也要記住,與scanf類型的函數此輸入會成功,返回1.

"1rubbish"

我想你可能想要的是像下面這樣的值:

// Somewhere near the top 
#include <stderr.h> 
// ... other includes 

const char* inname = "trianglein.txt"; 
const char* outname = "triangleout.txt"; 

// Any other stuff 


// Inside main... 

// Initialization of pointer and opening of file trianglein.txt 
if ((inptr = fopen(inname,"r")) == 0){ 
    fprintf(stderr, "Error opening file %s: %s", inname, strerror(inname)); 
    return -1; 
} 

// Initialization of pointer and opening of file triangleout.txt 
if ((outptr = fopen(outname,"w")) == 0){ 
    fprintf(stderr, "Error opening file %s: %s", outname, strerror(outname)); 
    return -1; 
} 


int result; 
while(true){ 
    result = fscanf(inptr,"%d %d %d",&a, &b, &c); 
    if (result == EOF) 
    break; 

    if (result < 3) // Ignore incomplete lines 
    continue; 

    // do the normal stuff 
} 
+0

儘管「針對NULL的測試」在語義上是正確的,並且您的代碼正確使用了「if(!inptr)」,但兩者並不匹配。 'if(input!= NULL)'明確地匹配,但幾乎沒有人會困擾。這個成語通常是'if(inptr = fopen(「trianglein.txt」)...',在測試中使用一個賦值。 –

+0

是的,我可能會這樣寫。 – Matt

0

您的程序在我的系統中正常工作。我在Windows 7上使用Code::Blocks 10.05

當文件trianglein.txt具有少於3個整數值並由fscanf()讀取時,會發生邏輯錯誤的唯一可能性。例如trianglein.txt文件的值爲1,1 2,1 2 3 4,1 2 3 4 5等會給變量b和/或c給出不正確的值。因此,在執行循環的每次迭代之前初始化a=-1,b=-1,c=-1,並在讀取後檢查它們。

如果您正在運行程序,請檢查文件triangleout.txt的訪問權限。有時候你可能沒有寫特定文件的權限。

順便說一下,分類邏輯是錯誤的。等邊三角形不能是直角的。