C语言文件

任务一、二

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 int main()
{
        FILE *fp = fopen("loss.txt", "w");
        if (fp == NULL){    
            printf("Failed to open file");
            return 0;
    }
    double i, y;
    for (i = 0, y = 0; i < 100; i += 0.5){
        fprintf(fp, "%f\t", i);
        y = sin(i);
        fprintf(fp, "%f\n", y);
    }
    fclose(fp);
     //FILE *fpread = fopen("loss.txt", "r");    
        //if (fpread == NULL)
    //{
    //    printf("Failed to open file ");
    //    return 0;
    //}
    ////int a[10] = { 0 };
    //int *a = new int[10];
    //for (int i = 0; i < 10; i++)
    //{
    //    fscanf(fpread, "%d", &a[i]);
    //    printf("%d ", a[i]);
    //}
    //fclose(fpread);
    //system("pause");
}

任务三

#include<stdio.h>
int main()
{
        int a[10]={0};
    FILE *fpRead=fopen("data.txt","r");
    if(fpRead==NULL){
        return 0;
    }
    for(int i=0;i<10;i++){
        fscanf(fpRead,"%d ",&a[i]);    
            printf("%d ",a[i]);
    }
    getchar();//等待
     return 1;
}

(用数字代替了)

任务四

system("cls"):清除屏幕,使文件的内容全部删除,便于重新编辑。

system("pause"):暂停,可以让用户看清楚输出的内容。

posted @ 2019-12-22 11:08  唐五  阅读(172)  评论(0编辑  收藏  举报