②引数で取得したファイル名のファイルから同じく引数で取得した個数の学生番号と氏名のデータを構造体変数に格納し、学生番号順に表示する関数。
上記の2つの関数を使用してプログラムを作りたいのですがn個の読み込みとファイル内のソートが分かりません。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student{
int id;
char name[20];
};
/*データファイル作成関数*/
int mk_nfile()
{
int i,i_temp;
char c_temp[20];
FILE *fp;
if((fp=fopen("name.txt","w"))==NULL){
printf("file open error.\n");
exit(1);
}
for(i=0;i<5;i++){
scanf("%d",&i_temp);
scanf("%s",c_temp);
fprintf(fp,"%d ",i_temp);
fprintf(fp,"%s\n",c_temp);
}
fclose(fp);
return (0);
}
/*読み込み・並び替え関数*/
int read_sort()
{
struct student member[5],st_temp;
struct student *ptr;
int i,i_temp;
int j,k;
char c_temp[30];
FILE *fp;
if((fp=fopen("name.txt","r"))==NULL){
printf("file open error\n");
exit(1);
}
/*構造体変数へ代入*/
ptr=member;
for(i=0;i<5;i++){
fscanf(fp,"%d",&i_temp);
(ptr+i)->id=i_temp;
fscanf(fp,"%s",c_temp);
strcpy((ptr+i)->name,c_temp);
}
fclose(fp);
/*並び替え処理*/
}
main()
{
mk_nfile();
read_sort();
}