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个浮点数。如果你的文件格式与这个不匹配,就会导致读取失败。

以下是一些可能的问题和解决方法:

  1. 检查文件内容:确保你的数据文件中的每一行都包含6个浮点数。如果文件中有其他类型的数据或空格不足,可能会导致读取失败。

  2. 调试输出:在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);
    
  3. 文件路径检查:确保文件路径正确,并且文件存在。你可以尝试在代码中添加一些日志信息来调试文件打开操作:

    复制代码
    printf("Opening file: %s\n", "C:\\Users\\YA\\source\\data.txt");
    
  4. 错误处理:确保在读取失败时,有适当的错误处理。你可以使用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编程问答网回答,权益归原著者所有;
 
下一篇:#include <stdio.h> #include <stdlib.h> #include <time.h> //结构体初始化 typedef struct Water_data { float pH; float Dissol_O; //水中溶解氧 float Dissol_N; //水中氮 float Dissol_P; //水中磷 float salt; //盐度 float water_tem; //水温 struct Water_data* next; }data; //释放链表空间函数 void freelink(data* head) { data* node = head; data* tail = NULL; while (node != NULL) { tail = node; node = node->next; free(tail); } head->next= NULL; //保留头节点 } //构建链表模拟收集数据函数 int collect(data* head1) { data* node, * tail; tail = head1; node = head1; tail->next = NULL; srand((unsigned int)time(NULL)); // 初始化随机数种子 int n; printf("输入需要采集的数据个数:"); scanf_s("%d", &n); for (int i = 0; i < n; i++) { node = (data*)malloc(sizeof(data)); if (node == NULL) { // 处理内存分配失败的情况 fprintf(stderr, "分配内存失败\n"); return NULL; } tail->next = node; //尾插法连接链表 tail = node; tail->next = NULL; } node = head1->next; while (node != NULL) { node->pH = (float)(rand() % 1400) / 1000.0 + 7.0; node->Dissol_O = (float)(rand() % 10) + 5.0; node->Dissol_N = (float)(rand() % 10) + 5.0; node->Dissol_P = (float)(rand() % 10) + 5.0; node->salt = (float)(rand() % 40) + 10.0; node->water_tem = (float)(rand() % 30) + 10.0; node = node->next; } //打印已采集的数据 int count = 1; //数据排序记录 node = head1->next; while (node != NULL) { printf("第%d组数据\n", count); printf("水体pH值:%0.2f\n", node->pH); printf("水中溶解氧:%0.2f\n", node->Dissol_O); printf("水中氮含量:%0.2f\n", node->Dissol_N); printf("水中磷含量:%0.2f\n", node->Dissol_P); printf("盐度:%0.2f\n", node->salt); printf("水体温度:%0.2f\n", node->water_tem); printf("\n"); node = node->next; count++; } node = head1->next; return count; } //修改数据函数 void correct(data* head, int b) { data* node; node = head->next; if (node == NULL) { printf("未赋值\n"); } else { int n; node = head->next; int num = 0; printf("输入需要修改的数组的序号:"); scanf_s("%d", &n); if (n > 0 && n < b) { while (num < n-1) { node = node->next; num++; } printf("修改pH值:"); scanf_s("%f", &node->pH); printf("修改水中溶解氧:"); scanf_s("%f", &node->Dissol_O); printf("修改水中氮含量:"); scanf_s("%f", &node->Dissol_N); printf("修改水中磷含量:"); scanf_s("%f", &node->Dissol_P); printf("修改盐度:"); scanf_s("%f", &node->salt); printf("修改水温:"); scanf_s("%f", &node->water_tem); printf("\n"); //打印修改后数据 printf("修改后:\n"); printf("水的pH值:%0.2f\n", node->pH); printf("水中溶解氧:%0.2f\n", node->Dissol_O); printf("水中氮含量:%0.2f\n", node->Dissol_N); printf("水中磷含量:%0.2f\n", node->Dissol_P); printf("盐度:%0.2f\n", node->salt); printf("水温:%0.2f\n", node->water_tem); } else { printf("输入错误\n"); } } } //插入数据函数 int insert(data* head, int b) { data* node,*tail,*news; tail = head; node = head->next; news = (data*)malloc(sizeof(data)); if (news == NULL) { // 处理内存分配失败的情况 fprintf(stderr, "分配内存失败\n"); return NULL; } news->pH = (float)(rand() % 1400) / 1000.0 + 7.0; news->Dissol_O = (float)(rand() % 10) + 5.0; news->Dissol_N = (float)(rand() % 10) + 5.0; news->Dissol_P = (float)(rand() % 10) + 5.0; news->salt = (float)(rand() % 40) + 10.0; news->water_tem = (float)(rand() % 30) + 10.0; int num = 0; if (node == NULL) { printf("未赋值\n"); } else { int n; int num = 1; printf("输入需要插入的数据后的序号:"); scanf_s("%d", &n); if (n > 0 && n < b) { while (num < n) { printf("执行%d次\n", num); node = node->next; num++; } tail = node->next; node->next = news; news->next = tail; } } node = head->next; return 0; } //删除数据函数 void delet_data(data* head, int b) { data* node, * tail; node = head; tail = head; int num = 0; int a = 0; if (node->next== NULL) { printf("未赋值\n"); } else { int n; int num = 1; printf("输入需要删除的数组的序号:"); scanf_s("%d", &n); if (n > 0 && n < b) { while (num <n) { node = node->next; num++; } tail = node->next; node->next = tail->next; tail->next = NULL; free(tail); } else { printf("输入错误\n"); } node = head->next; } } //检测数据函数 void alarm(data* head) { if (head->next == NULL) { printf("未赋值\n"); } else { int count = 1; data* node; node = head->next; while (node != NULL) { if (node->pH < 7 || node->pH>8) { printf("第%d组水体pH异常\n", count++); } else { printf("第%d组水体pH正常\n", count++); } node = node->next; } node = head->next; } } //获得养殖方式函数 void method(data* head) { int ways[4] = { 1,2,3,4 }; } //遍历数据函数 void print_data(data* head) { data* node=head->next; if (node == NULL) { printf("未赋值\n"); } else { int count = 1; //数据排序记录 node = head->next; while (node != NULL) { printf("第%d组数据\n", count); printf("水体pH值:%0.2f\n", node->pH); printf("水中溶解氧:%0.2f\n", node->Dissol_O); printf("水中氮含量:%0.2f\n", node->Dissol_N); printf("水中磷含量:%0.2f\n", node->Dissol_P); printf("盐度:%0.2f\n", node->salt); printf("水体温度:%0.2f\n", node->water_tem); printf("\n"); node = node->next; count++; } node = head->next; } } //数据录入文件函数 void input_File(data* head) { data* node; node = head->next; if (node == NULL) { printf("未赋值\n"); } else { FILE* fp; errno_t et = fopen_s(&fp, "C:\\Users\\YA\\source\\data.txt", "w"); if (fp == NULL) { printf("不能打开文件!\n"); exit(1); } node = head->next; int num = 1; while (node != NULL) { fprintf(fp, "第%d组数据\n",num++); fprintf(fp, "水的pH值:"); fprintf(fp, "%0.2f\n",node->pH);// 将每个节点的数据写入文件 fprintf(fp, "水中溶解氧:"); fprintf(fp, "%0.2f\n", node->Dissol_O); fprintf(fp, "水中氮含量:\n"); fprintf(fp, "%0.2f\n", node->Dissol_N); fprintf(fp, "水中磷含量:\n"); fprintf(fp, "%0.2f\n", node->Dissol_P); fprintf(fp, "盐度:\n"); fprintf(fp, "%0.2f\n", node->salt); fprintf(fp, "水温:\n"); fprintf(fp, "%0.2f\n", node->water_tem); node = node->next; } node = head->next; fclose(fp); printf("数据已录入文件\n"); freelink(head); } } 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; } //功能跳转 void Keydown(data* head) { int y = 10; int x = 0; while (y != 0) { printf("----水体数据采集管理系统----\n"); printf("\t0.退出系统\n"); printf("\t1.获取数据\n"); printf("\t2.修改数据\n"); printf("\t3.插入数据\n"); printf("\t4.删除数据\n"); printf("\t5.检测数据\n"); printf("\t6.养殖方式建议\n"); printf("\t7.遍历数据\n"); printf("\t8.数据录入文件\n"); printf("\t9.读取文件数据\n"); printf("----------------------------\n"); printf("输入0~9:"); scanf_s("%d", &y); switch (y) { case 0: printf("【已退出系统】\n"); break; case 1: printf("【获取数据】\n"); x = collect(head); break; case 2: printf("【修改数据】\n"); correct(head, x); break; case 3: printf("【插入数据】\n"); insert(head, x); break; case 4: printf("【删除数据】\n"); delet_data(head, x); break; case 5: printf("【检测数据】\n"); alarm(head); break; case 6: printf("【方式建议】\n"); break; case 7: printf("【遍历数据】\n"); print_data(head); break; case 8: printf("【数据录入文件】\n"); input_File(head); break; case 9: printf("【读取文件数据】\n"); break; default: printf("输入错误\n"); } int b; printf("是否清屏(1.是 0.否):"); scanf_s("%d", &b); if (b == 1 || b == 0) { if (b == 1) { system("cls"); } else { continue; } } else { printf("输入错误\n"); } } } //主函数 int main() { data* head = (data*)malloc(sizeof(data)); if (head == NULL) { // 处理内存分配失败的情况 fprintf(stderr, "分配内存失败\n"); return NULL; } head->next = NULL; Keydown(head); free(head); return 0; } 为什么读取文件数据无法赋值给链表