以下代码有什么问题?我该如何纠正?
数据以逗号分隔:姓名,地址,身份证号,年龄。
我想要的是从文本文件中读取以上四个字段,并在一行中用逗号分隔,并将这些数据存储在下面以代码形式给出的数组结构中,作为struct person
的实例:>
#include <stdio.h>
// struct person with 4 fields
struct person
{
char name[100];
char address[100];
char IDnumber[20];
int age;
};
int main()
{
FILE *file = fopen ("personout.txt","r");
// declares an struct array to store data
struct person student[10];
int k=0;
if ( file != NULL )
{
char line [ 128 ]; /* or other suitable maximum line size */
while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
{
// Returns first token
char* token = strtok(line,",");
// Keep printing tokens while one of the
// delimiters present in str[].
while (token != NULL) {
k=0;
// countword++;
printf("%s\n", token);
switch (k) {
case '1':
student[k].name==token;
case '2':
student[k].address==token;
case '3':
student[k].IDnumber==token;
case'4':
student[k].age==token;
default:
break;
}
k++;
// put the values to array
token = strtok(NULL, ",");
}
}
printf("%s\n", student[k].name);
fclose ( file );
}
}