2010-12-01 24 views
1

我已經中稱爲文本文件如下:values.txtc語言:讀取文件內容數字和它們加起來

1 4 
2.5 3.76 
122 10 
277.543 
165.4432 

我想讀這個文本文件的內容,並添加每個兩個對共同並輸出結果...... 輸出將是這樣的:

1 Pair:(1, 4) = 5 

2 Pair:(2.5, 3.76)= 6.26 

等..

我打開文件這樣

int c; 
FILE myfile; 

myfile= fopen("values.txt", "r"); 
if (myfile == NULL) { 
    printf("Cannot open TEXT file\n"); 
    return 1; 
} 

double aa,bb; 
while ((c = getc(myfile)) != EOF) { 
    // HERE SHOULD I DO THE OUTPUT BUT HOW? 
} 

任何幫助,真是感激..

語言= C

+0

這是作業嗎? – 2010-12-01 01:14:52

回答

0

對於這個簡單的任務,使用

double a, b; 
if (fscanf(myfile, "%lf %lf", &a, &b) == 2) 
     printf("%f + %f = %f\n", a, b, a+b);

0

看起來像一個家庭作業的問題,但fscanf可以閱讀串入一個變量,如:

int n; 
fscanf (myfile,"%d",&n); 
+0

不,它不是一項功課... iam試圖計算從電影中提取的幀之間的時間差... – cprogram 2010-12-01 01:30:43

+0

由於一些數字似乎是分數,`%d`格式和`int`類型可能不是最佳匹配。 – 2010-12-01 03:06:29

2

下面的代碼做了什麼,你的期望。 myfile應該聲明爲FILE *。 fopen返回一個指向FILE結構的指針。如果文件非常大,我會建議在大尺寸的緩衝區中讀取數據(例如:65535等),並通過char解析char並將其轉換爲浮點值。它減少了系統調用的開銷,比處理文本浮動值花費更多的時間。

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

main(int argc, char *argv[]) 
{ 
    FILE* myfile; 

    myfile = fopen("values.txt", "r"); 
    if (myfile == NULL) { 
     printf("Cannot open TEXT file\n"); 
     return 1; 
    } 

    double aa,bb; 
    while (2 == fscanf(myfile, "%lf %lf", &aa, &bb)) { 
     printf("%lf\n", aa+bb); 
    } 
    return 0; 
} 
0

您還沒有表現出你所需要的作爲單值線輸出,但是這看起來像fgets()sscanf()的情況下,除非你真的想用一個值的兩行被處理爲一個單位。

char buffer[256]; 
int rownum = 0; 
while (fgets(buffer, sizeof(buffer), myfile) != 0) 
{ 
    double aa, bb; 
    int n = sscanf(buffer, "%lf %lf", &aa, &bb); 
    if (n == 2) 
     printf("%d Pair:(%g, %g) = %g\n", ++rownum, aa, bb, aa+bb); 
    else if (n == 1) 
     printf("%d Solo:(%g) = %g\n", ++rownum, aa, aa); 
    else 
    { 
     printf("Failed to find any numbers in <<%s>>\n", buffer); 
    } 
} 

如果使用fscanf(myfile, "%g %g", &aa, &bb),那麼它會讀一遍換行符(他們算作空格)尋找數字,所以它會讀取從一條線一個號碼,然後從另一行第二。這通常不是人們以後的事情(但是當它是你需要的時候,它是非常有用的)。使用fscanf()的錯誤恢復往往比使用fgets()sscanf()更爲充實。

0

其在C++對不起:(我不知道的C

這是一個簡單的民德一個非常簡單的邏輯代碼:D我是一個begineer過,如果出了問題我還沒有測試這種前衛很抱歉,但是,究竟 一個相同的原則是我的解析器工作,它工作得很好,所以這是一個真正的方法,不是很有效,但... 不要立即使用這個程序,理解它的邏輯,這將幫助你很多,複製,不會給你任何東西 ...解析器導師是如此罕見... ...

int x = 0; char ch ='r'; // i'v用這個equasion來避免呃在第一個cckck ror。 程序啓動時,它必須由某些東西填充。 char bigch [10]; int checknumber = 0;

float firstnumber = 0; float secondnumber = 0; float result = 0; (char frombigar [10],int xar)//此函數獲取bigch作爲參考,這意味着eny 在此處所做的更改將直接影響bigch本身。 該函數獲取數組的實際長度,並將空間 放在bigch的每個元素中以將數字清零。我們需要清除 任何以前的數字bigch。下面你會明白爲什麼我需要這個。 'xar'是主函數的x。它在這裏告訴我們更清潔的 填充bigar元素的真實長度。 { 對(INT I = 0;我 }

}

INT主() { < -------------------//在這裏添加文件打開和讀取命令 while(!myfile.eof())// txt文件的末尾還沒有到達 { ch = myfile.get(); //將每個字母都放入ch中,並且使光標向前移動一步 在txt文件中進一步閱讀 get()是否自動轉發光標

if (ch!= " ")     //i used space as an indicator where one number ends 
            //so while space havent been reahced, read letters. 
    { bigch[x] = ch;    //get read letter into bigch array. 
     x++;      //icrement bigch array step 

    } 
else 

if(ch == " ")    //if space is reached that means one number has ended and 
    {      im trying to set a flag at that moment. it will be used further. 
    checknumber++;   the flag is simple number. first space will set checknumber to 1 
          second space will set it to 2. thats all. 
    } 

    if (checknumber == 1)      //if our checknumber is 1, wich means that reading 
              of first number is done, lets make one whole float    
              from that bigch array. 

{firstnumber = atof(bigch); //這裏我們得到了bigch,atof(數組到浮點)命令將 bigch數組轉換爲一個完整的浮點數。

clearar(bigch,x);        //here we send bigch and its element step into function where 
               bigch gets cleaned because we dont want some ghost numbers in it. 
               abviously clearar function cleans bigch int main function aswell, 
                not only in it's teritory. its a global cleaning :) 
     } 
    else if (checknumber ==2)      //here we do the same but if flag is 2 this means that two spaces 
                had been passed and its time to convert bigch into secondnumber. 
      { secondnumber = atof(bigch);   //same method of converting array into float (it hates other 
                not number letters, i mean if its a number its fine. if in your text 
                was 'a' or 's' in that case atof will panic hehe..) 
      clearar(bigch,x);      //same thing, we send bigch to cleaner function to kill any numbers 
                it, we get one space letter (" ") into each element of bigch. 
      } 

checknumber = 0;如果兩個數字都被讀出並轉換。我們需要重置 空間標記。並從0開始計數;爲下一對號碼。

result = firstnumber + secondnumber;在這裏,一切都很清楚。 } }