这个程序在函数loadFromFile()中尝试从文件中加载用户信息和设备信息,但是存在一些问题导致无法成功加载。

问题出在loadFromFile()函数中的文件读取部分。在加载用户信息时,程序会一直读取文件直到文件结束,然后尝试加载设备信息。由于已经到达文件结束,再次尝试读取设备信息时会立即返回,因此设备信息无法加载成功。

为了解决这个问题,可以将加载设备信息的代码移动到加载用户信息的代码之后。这样,先加载用户信息,然后再加载设备信息。这样修改后的代码如下:

// 从文件中加载用户信息和设备信息
void loadFromFile() 
{
    FILE* file = fopen("C:\\Users\\额额\\Desktop\\cvb\\789", "r");
    if (file == NULL) 
	{
        printf("无法打开文件。\n");
        return;
    }

    // 加载用户信息
    while (!feof(file)) {
        fscanf(file, "%s %s", users[numUsers].username, users[numUsers].password);
        numUsers++;
    }

    // 关闭文件
    fclose(file);

    // 打开文件以加载设备信息
    file = fopen("C:\\Users\\额额\\Desktop\\cvb\\789", "r");
    if (file == NULL) 
	{
        printf("无法打开文件。\n");
        return;
    }

    // 跳过用户信息
    while (!feof(file)) {
        char buffer[100];
        fgets(buffer, 100, file);
    }

    // 加载设备信息
    while (!feof(file)) {
        fscanf(file, "%s %s %s %s %s %s %s %s", devices[numDevices].name, devices[numDevices].model,
            devices[numDevices].purchase_date, devices[numDevices].location, devices[numDevices].owner,
            devices[numDevices].borrower, devices[numDevices].borrow_date, devices[numDevices].return_date);
        numDevices++;
    }

    // 关闭文件
    fclose(file);
}

这样修改后的程序应该能够成功加载文件中的内容

#define _CRT_SECURE_NO_WARNINGS#include stdioh#include stdlibh#include stringh#define MAX_DEVICES 100 最大设备数量#define MAX_USERS 100 最大用户数量 设备信息结构体typedef struct char name50; char model50;

原文地址: https://www.cveoy.top/t/topic/hJda 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录