- ถ้าต้องการจัดรูปแบบข้อความ สามารถใช้ formatted output ได้ในโหมดข้อความ (text mode ) . ปกติการจัดรูปแบบก็เพื่อนให้ผู้ใช้งานได้อ่านทีหลัง. โดยทั่วไปแล้วจะไม่ทำการจัดรูปแบบเพื่อนำกลับมาใช้ในภาษาซีอีกครั้ง
- ควรปิดไฟล์ทุกครั้งเมื่อเสร็จสิ้นการใช้งาน
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
int main(void) | |
{ | |
char *fileName = "random.txt"; | |
srand(time(0)); | |
setbuf(stdout,NULL); | |
FILE *fout = fopen(fileName,"a"); | |
if(fout == NULL)exit(-1); | |
int i,num; | |
for(i = 0 ; i < 100 ; i++) | |
{ | |
num =rand()%100; | |
printf("%d ",num); | |
fprintf(fout,"%d \n",num); | |
} | |
fclose(fout); | |
FILE *fin = fopen(fileName,"r"); | |
//int num; | |
while(1) | |
{ | |
fscanf( fin,"%d", &num); | |
printf("%d\n",num); | |
if(feof(fin)) break; | |
} | |
fclose(fin); | |
return 0; | |
} |