标签:close failed scanf += 文件 暂停 fopen oss class
任务一、二
#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"):暂停,可以让用户看清楚输出的内容。
标签:close failed scanf += 文件 暂停 fopen oss class
原文地址:https://www.cnblogs.com/wjl1394270327/p/12079259.html