Ken H

Ken H

  • NA
  • 646
  • 361k

How to get the field value is assigned to the variables of the structure in .txt file

Jun 27 2012 4:41 AM
hi all,
  i have a file name for StuInfo.txt,It has the following content:
stuName stuNum stuProfession  EnglishScore  NatureScore
 Ken 2003001 Soft-Eng  98.5  88.5
 FoxH 2003002 Database-Eng  90  75.5
......

.cpp code:
#include<stdio.h>
#include <stdlib.h>
#include <iostream.h>
#define SIZE 20
struct Student_Score
{
 char stuName[10];
  int stuID;
 char stuPro[10];
 float EnglishScore;
 float NatureScore;
 }stu_info[SIZE];

int main(void)
{
 FILE *fp;
 int i;
 char fname[128]="StuInfo.txt";
 if((fp=fopen(fname, "r"))==NULL) {
 printf("Cannot open file.\n");
 exit(1);
 }
 for (i=0;i<SIZE;i++)
 {
 fread(&stu_info[i],sizeof(Student_Score),1,fp);
 printf("%s\n",stu_info[i].stuName); // now,I just want to get  name. how do?
 }
 fclose(fp);
 
 return 0;
}
thank very much.

Answers (1)