#include "stdafx.h"
#include <stdlib.h>/*为了调用system("PAUSE");*/
#include <time.h>/*为了调用time;*/
#include <stdio.h>
#include <string.h>
#include <windows.h>
struct student_type
{
char name[10];
int num;
int age;
}stud[70];
void save(char *name, int n)
{
FILE *fp;
int i;
if((fp=fopen(name,"wb"))==NULL)
{
printf("can't open file!\n");
exit(0);
}
for(i=0; i<n; i++)
{
if(fwrite(&stud[i],sizeof(struct student_type), 1, fp)!=1)
{
printf("file write error\n");
}
}
fclose(fp);
}
void main(int argc , char* argv[])
{
int i,n;
FILE *fp;
char filename[50];
printf("Input filename :\n");
scanf("%s",filename);
printf("Input number of students :\n");
scanf("%d",&n);
printf("Input name, number, age:\n");
for(i=0; i<n; i++)
{
scanf("%s%d%d", stud[i].name, &stud[i].num, &stud[i].age);
}
save(filename, n);
if((fp = fopen(filename, "rb"))==NULL)
{
printf("can't open file!! \n");
exit(0);
}
for(i=0; i<n; i+=2)
{
fseek(fp,i*sizeof(struct student_type),0);
fread(&stud[i], sizeof(struct student_type),1,fp);
printf("%-10s%5d%5d\n",stud[i].name,stud[i].num, stud[i].age);
}
fclose(fp);
system("PAUSE");
return C_SYS_OK;
}
#include <stdlib.h>/*为了调用system("PAUSE");*/
#include <time.h>/*为了调用time;*/
#include <stdio.h>
#include <string.h>
#include <windows.h>
struct student_type
{
char name[10];
int num;
int age;
}stud[70];
void save(char *name, int n)
{
FILE *fp;
int i;
if((fp=fopen(name,"wb"))==NULL)
{
printf("can't open file!\n");
exit(0);
}
for(i=0; i<n; i++)
{
if(fwrite(&stud[i],sizeof(struct student_type), 1, fp)!=1)
{
printf("file write error\n");
}
}
fclose(fp);
}
void main(int argc , char* argv[])
{
int i,n;
FILE *fp;
char filename[50];
printf("Input filename :\n");
scanf("%s",filename);
printf("Input number of students :\n");
scanf("%d",&n);
printf("Input name, number, age:\n");
for(i=0; i<n; i++)
{
scanf("%s%d%d", stud[i].name, &stud[i].num, &stud[i].age);
}
save(filename, n);
if((fp = fopen(filename, "rb"))==NULL)
{
printf("can't open file!! \n");
exit(0);
}
for(i=0; i<n; i+=2)
{
fseek(fp,i*sizeof(struct student_type),0);
fread(&stud[i], sizeof(struct student_type),1,fp);
printf("%-10s%5d%5d\n",stud[i].name,stud[i].num, stud[i].age);
}
fclose(fp);
system("PAUSE");
return C_SYS_OK;
}