int read_data(data* head) { FILE* fp; data* node = head; errno_t et = fopen_s(&fp, "C:\\Users\\YA\\source\\data.txt", "r"); if (fp == NULL) { perror("不能打开文件"); return 1; } while (1) { data* new_node = (data*)malloc(sizeof(data)); if (new_node == NULL) { fprintf(stderr, "分配内存失败\n"); fclose(fp); return 1; } new_node->next = NULL; int result = fscanf_s(fp, "%f %f %f %f %f %f", &new_node->pH, &new_node->Dissol_O, &new_node->Dissol_N, &new_node->Dissol_P, &new_node->salt, &new_node->water_tem); if (result == EOF) { free(new_node); break; } else if (result != 6) { perror("读取文件失败"); free(new_node); fclose(fp); return 1; } node->next = new_node; node = new_node; } fclose(fp); return 0; } 为什么文件数据无法赋值给链表
int read_data(data* head) { FILE* fp; data* node = head; errno_t et = fopen_s(&fp, "C:\\Users\\YA\\source\\data.txt", "r"); if (fp == NULL) { perror("不能打开文件"); return 1; } while (1) { data* new_node = (data*)malloc(sizeof(data)); if (new_node == NULL) { fprintf(stderr, "分配内存失败\n"); fclose(fp); return 1; } new_node->next = NULL; int result = fscanf_s(fp, "%f %f %f %f %f %f", &new_node->pH, &new_node->Dissol_O, &new_node->Dissol_N, &new_node->Dissol_P, &new_node->salt, &new_node->water_tem); if (result == EOF) { free(new_node); break; } else if (result != 6) { perror("读取文件失败"); free(new_node); fclose(fp); return 1; } node->next = new_node; node = new_node; } fclose(fp); return 0; } 为什么文件数据无法赋值给链表
int read_data(data* head) {
FILE* fp;
data* node = head;
errno_t et = fopen_s(&fp, "C:\\Users\\YA\\source\\data.txt", "r");
if (fp == NULL) {
perror("不能打开文件");
return 1;
}
while (1) {
data* new_node = (data*)malloc(sizeof(data));
if (new_node == NULL) {
fprintf(stderr, "分配内存失败\n");
fclose(fp);
return 1;
}
new_node->next = NULL;
int result = fscanf_s(fp, "%f %f %f %f %f %f",
&new_node->pH,
&new_node->Dissol_O,
&new_node->Dissol_N,
&new_node->Dissol_P,
&new_node->salt,
&new_node->water_tem);
if (result == EOF) {
free(new_node);
break;
}
else if (result != 6) {
perror("读取文件失败");
free(new_node);
fclose(fp);
return 1;
}
node->next = new_node;
node = new_node;
}
fclose(fp);
return 0;
} 为什么文件数据无法赋值给链表

用户405587875
2025年03月05日
|
浏览 131
fscanf_s函数在读取数据时,需要确保参数的数量和类型正确匹配。在你的代码中,fscanf_s的格式字符串是"%f %f %f %f %f %f",这意味着它期望读取6个浮点数。如果你的文件格式与这个不匹配,就会导致读取失败。
以下是一些可能的问题和解决方法:
-
检查文件内容:确保你的数据文件中的每一行都包含6个浮点数。如果文件中有其他类型的数据或空格不足,可能会导致读取失败。
-
调试输出:在
fscanf_s之后添加一个调试输出语句,打印出读取的结果,以确保每次读取的项数正确。例如:复制代码printf("Read values: %f, %f, %f, %f, %f, %f\n", new_node->pH, new_node->Dissol_O, new_node->Dissol_N, new_node->Dissol_P, new_node->salt, new_node->water_tem); -
文件路径检查:确保文件路径正确,并且文件存在。你可以尝试在代码中添加一些日志信息来调试文件打开操作:
复制代码printf("Opening file: %s\n", "C:\\Users\\YA\\source\\data.txt"); -
错误处理:确保在读取失败时,有适当的错误处理。你可以使用
perror来输出错误信息,并检查返回值以确定读取是否成功。
以下是一个改进后的代码示例,包含调试输出和错误处理:
复制代码
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
typedef struct data {
float pH;
float Dissol_O;
float Dissol_N;
float Dissol_P;
float salt;
float water_tem;
struct data* next;
} data;
int read_data(data* head) {
FILE* fp;
data* node = head;
errno_t et = fopen_s(&fp, "C:\\Users\\YA\\source\\data.txt", "r");
if (fp == NULL) {
perror("不能打开文件");
return 1;
}
while (1) {
data* new_node = (data*)malloc(sizeof(data));
if (new_node == NULL) {
fprintf(stderr, "分配内存失败\n");
fclose(fp);
return 1;
}
new_node->next = NULL;
int result = fscanf_s(fp, "%f %f %f %f %f %f",
&new_node->pH,
&new_node->Dissol_O,
&new_node->Dissol_N,
&new_node->Dissol_P,
&new_node->salt,
&new_node->water_tem);
if (result == EOF) {
free(new_node);
break;
} else if (result != 6) {
perror("读取文件失败");
free(new_node);
fclose(fp);
return 1;
}
node->next = new_node;
node = new_node;
}
fclose(fp);
return 0;
}
请检查你的数据文件格式,并确保每次读取的项数正确。如果问题仍然存在,请提供更多的错误信息或文件内容,以便进一步排查。
我要提问
复制内容
分享给好友
AI编程问答网 免责声明:
以上内容除特别注明外均来源于网友提问,AI编程问答网回答,权益归原著者所有;
