#include
void main()
{
int number;
//-----Create a write to file----
FILE *f = fopen("D:\\test.txt","w");//w write, a append, r read
puts("enter number (ctrl z to end):");
scanf("%d",&number);
while(!feof(stdin)){
fprintf(f,"%d\n",number);
puts("enter number (ctrl z to end):");
scanf("%d",&number);
}
fclose(f);
//-----Read from a file----
f = fopen("D:\\test.txt","r");//w write, a append, r read
fscanf(f,"%d",&number);
while(!feof(f)){
printf("%d\n",number);
fscanf(f,"%d",&number);
}
fclose(f);
}
//Complete version
#include "stdio.h"
typedef struct student{
int id;
char name[64];
}STUDENT;
void main()
{
STUDENT csc202;
//-----Create a write to file----
FILE *f = fopen("D:\\student.txt","w");//w write, a append, r read
puts("enter student id (ctrl z to end):");
scanf("%d",&csc202.id);
while(!feof(stdin)){
puts("enter student first name:");
scanf("%s",csc202.name);
fprintf(f,"%d %s\n",csc202.id,csc202.name);
puts("enter student id (ctrl z to end):");
scanf("%id",&csc202.id);
}
fclose(f);
//-----Read from a file----
f = fopen("D:\\student.txt","r");//w write, a append, r read
fscanf(f,"%d %s",&csc202.id,csc202.name);
while(!feof(f)){
printf("%d %s\n",csc202.id,csc202.name);
fscanf(f,"%d %s",&csc202.id,csc202.name);
}
fclose(f);
}