This page looks plain and unstyled because you're using a non-standard compliant browser. To see it in its best form, please visit upgrade to a browser that supports web standards. It's free and painless.

羅伯特學程式 會員登入 會員註冊

« 上一篇 | 下一篇 »

 

一個習題的練習…


<pre lang="c">
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>
#include <time.h>
#define MAX 10

int main(void)
{
    int f1,a,i;
    float sum=0.0;
    char str[3]={"0"},eol='',rtn='\n',tmp;
    srand(time(0));
    f1=creat("c:\\prog\\rand.txt",O_TEXT|S_IWRITE);
    if(f1!=-1)


    {
        for(i=0;i<MAX;i++)
        {
            a=rand()%64+1;
            itoa(a,str,10);
            write(f1,str,strlen(str));
            write(f1,&eol,1);
            write(f1,&rtn,1);
        }
        close(f1);
        printf("檔案寫入完成!!\n");       
    }
    else
        printf("檔案開啟失敗-1!!\n");

    f1=open("c:\\prog\\rand.txt",O_TEXT|O_RDONLY);
    if(f1!=-1)
    {
        for(i=0;i<MAX;i++)
        {
            read(f1,str,3);
            read(f1,&tmp,1);
            a=atoi(str);
            printf("%d\n",a);
            sum+=a;                       
        }
        close(f1);
        printf("平均數為 : %.1f\n",sum/10);   
    }
    else
        printf("檔案開啟失敗-2!!\n");

    system("pause");
    return 0;
}
</pre>

發表回應