[英]read data from file into array of structures
Hi I'm trying to read data from file into array of structures I tried to use fgets but got the error saying that RECORD type cannot be convert to char* this is what i have so far 嗨,我正在尝试从文件中读取数据到结构数组,我尝试使用fgets,但是出现错误,提示无法将RECORD类型转换为char *
#include <stdio.h>
#include <stdlib.h>
#define MAX_NAME 20
#define FILE_NAME 50
#define LIST_SIZE 50
//void getData(RECORD name[], RECORD score)
typedef struct RECORD
{
char *name;
float score;
}RECORD;
int main (void)
{
// Declarations
FILE *fp;
char fileName[FILE_NAME];
RECORD list[LIST_SIZE];
int count = 0;
// Statements
printf("Enter the file name: ");
gets(fileName);
fp = fopen(fileName, "r");
if(fp == NULL)
printf("Error cannot open the file!\n");
while (fgets(list[count], LIST_SIZE, fp) != NULL)
{
count++;
}
return 0;
}
the error occurs in the fgets statement inside while loop, how can i fix this and read data into array of structures? 错误在while循环内的fgets语句中发生,如何解决此问题并将数据读入结构数组?
thank in advance 预先感谢
Try this, 尝试这个,
while(fgets((char *)list[count], SIZE/* data size to be read */, fp) != NULL)
{...}
fgets is used to input a string from a text file one line as a unit. fgets用于从文本文件中以一行为单位输入字符串。
eg) 例如)
char input_line_buff[128];
fgets(input_line_buff, 128, fp);
read-in 读入
input_line_buff:(contents eg) "name 66.6\\n" input_line_buff :(内容例如)“名称66.6 \\ n”
you do split , memory alloc and copy, and convert . 您可以进行split,内存分配和复制以及convert。
eg) 例如)
list[count].name = strdup("name");
list[count].score= atof("66.6");
count++;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.