1. 程式人生 > >C語言建立與讀寫txt檔案

C語言建立與讀寫txt檔案

#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");
}