我一直在尝试使用以下格式的fscanf从文件中读取数据:
<!-- Don't use document.write() to add results to the page.
Instead, set up an HTML placeholder for results and populate
it dynamically. -->
<div>Negative Integers: <span id="negatives"></span></div>
<div>Sum of negatives: <span id="sum"></span></div>
读取功能:
fscanf(members, " %[^\n] %[^\n] %d %[^\n] %[^\n] %[^\n]", temp_node->name, temp_node->personal_names, &temp_node->mem_id, temp_node->email, temp_node->boat_class, temp_node->boat_name);
结构定义:
member_node read_member(FILE * members){
int scan = 0;
member *temp_node;
temp_node = calloc(1,sizeof(temp_node));
scan = fscanf(members, " %[^\n] %[^\n] %d %[^\n] %[^\n] %[^\n]", temp_node->name, temp_node->personal_names, &temp_node->mem_id, temp_node->email, temp_node->boat_class, temp_node->boat_name);
if(scan==EOF){
free(temp_node);
return NULL;
} else{
return temp_node;
}
}
当它开始读取第二行(personal_names)时,它会正确读取,但是不会停止,而是从头开始读入同一数组。
调试器中的输出:
typedef struct member{
char name[20];
char personal_names[80];
int mem_id;
char email[50];
char boat_class[30];
char boat_name[30];
}member;
预期输出:
name - Morris
personal_names - Huw Rhys MorganMorrisHuw Rhys Morgan 1015... util the array is full
mem_id - 1015
email - h.w.m.morris@\0David\n235\nd.price....
boat_class - noclub.club.uk...
boat_name - Flyer\0f.long...
文件具有以下格式:
name - Morris
personal_names - Huw Rhys Morgan
mem_id - 1015
email - h.w.m.morris@nolcub.clubs.uk
boat_class - Flyer
boat_name - The Blue One