全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:3089
推到 Plurk!
推到 Facebook!

C語言讀檔程式的問題?

尚未結案
tai1688
一般會員


發表:9
回覆:12
積分:4
註冊:2005-03-15

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-04-27 17:38:53 IP:71.104.xxx.xxx 未訂閱
#include
int main()
{
        char c;
    int n,count,a; 
        double sum,average;
        FILE *inptr,*outptr;
        count=0;
        sum=0.0;            inptr=fopen("lab13in.dat","r");
        if (inptr==NULL)
        {
          printf("Error opening file");
          }
          else
          {
          do 
           {
           c = fgetc(inptr);
           sum =1;
           if (c == ' ') 
            n  ;
      } while (c != EOF);
    }
    a=n 1;
    outptr=fopen("lab13out.dat","w");
    fwrite(outptr,inptr);
         if (a%8==0)
         {
          printf("\n");
         }
         fclose (inptr);
        fclose (outptr);
        average=sum/a;
        printf("The average of these integers is %d\n",average);
        printf("The file contains %d integers",count);
        system("pause");
        return 0;
}
(lab13in.dat 裡的數字) ------------------------------------------------------- 23.12 828 825.567 532 932 88 671 692 629 461 96 443 116 635 819.123456 912.97 481.1 264.067 829.0999 806.99 208.987 141.3 102.2 6.567 809.0 577.321 637.567 451.86755 369 18 41.01 767.6666 79.3333 882.9 963 980 820 288 830 433 491 293 184 162 478 1 555.01234 3.2111 -------------------------------------------------------------- 老師剛教檔案的輸入與輸出,所以有很多都不懂~我希望把lab13in.dat讀入後,把裡面的數字以每8個數字為一行另存為lab13out.dat,現在不知哪有錯~(因為不能用array,所以我用fgetc然後算出空格在加1來求數值~不知我有沒有做錯~ 求高手幫忙,小弟感積不盡~ 發表人 - tai1688 於 2005/04/27 17:47:43
blk5743
高階會員


發表:34
回覆:371
積分:236
註冊:2003-11-17

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-04-27 19:24:46 IP:61.66.xxx.xxx 未訂閱
tai1688你好:    參考一下這個方法 我把你的數值都存到陣列去 剩下你所要自己做的就是每8個數字放到同一行,寫到檔案去  相信你應該沒有問題    另外 如過你可以用BCB的函式,請參考 http://delphi.ktop.com.tw/topic.php?topic_id=62932 【問題】開檔後如何將字串內容寫進一陣列中
int main()
{
    float a[1200];
    int p = 0;
    char *token,msg[100];
    FILE *fp;        fp = fopen("lab13in.dat","r");
    ZeroMemory(a,sizeof(a));
    while ( fgets(msg, 100, fp) )
    {
        token=strtok(msg," ");//引號內填入你要用來區隔的字元" "
        while(token != NULL)
        {
            a[p  ] = atof(token);
            token = strtok(NULL," "); //引號內填入你要用來區隔的字元" "
        }
    }
    fclose(fp);
}
 
tai1688
一般會員


發表:9
回覆:12
積分:4
註冊:2005-03-15

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-04-28 01:07:12 IP:71.104.xxx.xxx 未訂閱
blk5743 你好, 我原本就想把它用陣列來做,但是老師說不可以用陣列,有其他的方法嗎?
windblown
中階會員


發表:0
回覆:117
積分:98
註冊:2003-11-14

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-04-28 09:27:19 IP:61.229.xxx.xxx 未訂閱
blk5743 已經提供了很好的範例了  你可以 >
blk5743
高階會員


發表:34
回覆:371
積分:236
註冊:2003-11-17

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-04-28 09:38:31 IP:61.66.xxx.xxx 未訂閱
引言: blk5743 已經提供了很好的範例了 你可以 1 開始時,同時開啟讀取 lab13in.dat 與寫入 lab13out.dat 的兩個檔案 2 利用一個「計數」的變數 nCount = 0, 如果 nCount 每讀一個數值時,就同時寫入 lab13out.dat 內, 且 nCount;直到 nCount>8 時,自動加上「換行」,而且 nCount 重新歸零 3 你要計算 sum 與 average 也可以同樣在裡面計算 加油!!
windblown厲害,我倒是沒想到這樣做 不過我補充一下Winblown的作法 基本上我們要把lab13in.dat讀入,並寫入 lab13out.dat 但是我們要每8個數字為一行,所以 1.我們在lab13in.dat中每讀入一個字,就寫到lab13out.dat 2.如果讀入的字有換行的符號(0x0d,0x0a),則不寫入lab13out.dat 以你的例子,可能只要不是數字,小數點,空白都要排除寫入lab13out.dat 3.每8個數字,在lab13out.dat加入換行(0x0d,0x0a)
tai1688
一般會員


發表:9
回覆:12
積分:4
註冊:2005-03-15

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-04-28 11:02:22 IP:71.104.xxx.xxx 未訂閱
大大们,像23.12 這樣才算一個數字,所以我必需算出空格,然後把空格一起輸出到outptr,還有我要如合把數字们加起來阿?下面是我改寫的code,它ㄉ輸出怪怪ㄉ~  
#include
int main()
{
    int n,count,c; 
        double sum,average;
        FILE *inptr,*outptr;
        count=0;
        sum=0.0;
        n=0;
        inptr=fopen("lab13in.dat","r");
        outptr=fopen("lab13out.dat","w");
        if (inptr==NULL)
        {
      printf("Error opening file");
     }
     else
     {
     do 
     {
      c = fgetc(inptr);
      fprintf(outptr,"%d",&c);
      fflush(outptr);
      if (c == ' ') /*這是要算空格ㄉ*/
      {
      n  ;
      }
      if (n=8)
      {
       fprintf(outptr,"\n");          }
      } while (c != EOF);
    }
    
    fclose (inptr);
        fclose (outptr);
        printf("The average of these integers is %d\n",average);
        printf("The file contains %d integers",count);
        system("pause");
        return 0;
}
 
GeorgeKu
中階會員


發表:1
回覆:120
積分:74
註冊:2004-10-13

發送簡訊給我
#7 引用回覆 回覆 發表時間:2005-04-28 13:17:50 IP:61.228.xxx.xxx 未訂閱
tai1688你好,    我把你的CODE做了一些修改如下:
#include
int main()
{
    char str[10];
    int n=0; 
    double sum=0.0,average=0.0;
    FILE *inptr,*outptr;
    inptr=fopen("lab13in.txt","r");
    outptr=fopen("lab13out.txt","w");
    if (inptr==NULL)
    {
      printf("Error opening file\n");
     }
     else
     {
     while (fscanf(inptr,"%s",str)!=EOF)
     {
      fprintf(outptr,"%s",str);
      fprintf(outptr," ");
      n  ;
      if (n%8==0)
      {
       fprintf(outptr,"\n");
      }
      sum=sum strtod(str,NULL);
      } 
      fclose (inptr);
      fclose (outptr);
      average=sum/n;
      printf("The average of these integers is %lf\n",average);
      printf("The file contains %d integers\n",n);
    }
    system("PAUSE");
    return 0;
}
windblown
中階會員


發表:0
回覆:117
積分:98
註冊:2003-11-14

發送簡訊給我
#8 引用回覆 回覆 發表時間:2005-04-28 13:49:10 IP:61.229.xxx.xxx 未訂閱
To tai1688:    1 建議使用 fgets() 來讀取一行的資料 2 利用 strtok() 來將空白分段,而且不管多少個空白都當成一個    結論 blk5743 大大的程式碼已經幾乎完成 80% 的工作了  >
tai1688
一般會員


發表:9
回覆:12
積分:4
註冊:2005-03-15

發送簡訊給我
#9 引用回覆 回覆 發表時間:2005-04-28 14:19:26 IP:71.104.xxx.xxx 未訂閱
因為我沒學過strtok()所以一直看不懂blk5743大大在做什麼,不過現在終於知到了
系統時間:2024-06-08 3:17:44
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!