c - 如何将文本文件传递到c中的数组中?

标签 c arrays file

菜鸟程序员在这里...所以请耐心等待。我试图将现有的文本文件传递到数组中,但是一旦我的主菜单加载该文件中曾经存在的信息就会消失,即使我没有进行任何更改。它只是不保留信息。该程序应该允许用户创建新文件或更新和/或加载现有文件。关于如何解决这个问题有什么想法吗?谢谢大家!

char fileName[20] = "";

void loadEmployee()
{
    FILE* fPtr;
    char singleLine[150];

    if (strcmp(fileName, "") == 0)
    {
        printf("\nWhat's the name of the file? ");
        scanf_s("%s", fileName, 20);
    }

    fopen_s(&fPtr, fileName, "r");

    while (!feof(fPtr))
    {
        fgets(singleLine, 150, fPtr);
        puts(singleLine);
    }
    fclose(fPtr);
}

void saveEmployee()
{
    FILE* fPtr;

    if (strcmp(fileName, "") == 0)
    {
        printf("\nWhat's the name of the file? ");
        scanf_s("%s", fileName, 20);
    }

    fopen_s(&fPtr, fileName, "w");

        for (int i = 0; i < numEmps; i++)
        {
            fprintf(fPtr, "%s %f %f\n", emps[i].emps.name, 
                        emps[i].emps.rate, emps[i].emps.hours);
        }
        fclose(fPtr);
}


void loadMenu()
{
    int i = 0;

    printf("1. Load from a file  \n");
    printf("2. Keyboard \n");
    scanf_s("%d", &choice);

    switch (choice)
    {
    case 1: loadEmployee();
        break;

    default:
        break;
    }

        do
        {
            printf("\nMAIN MENU\n");
            printf("1.Add Employee\n");
            printf("2.Edit Employee\n");
            printf("3.Print Employee\n");
            printf("4.Print ALL employees\n");
            printf("5.Exit\n");
            scanf_s("%d", &choice);

            switch (choice)
            {
            case 1: NameInput();
                break;
            case 2: printf("Choose employee: \n");
                for (int i = 0; i < numEmps; i++)
               {
                printf("%d. %s \n", i + 1, 
                                    emps[i].emps.name);
               }
                scanf_s("%d", &choice);

                empUpdate(choice - 1);

                break;

            case 3: printf("Choose employee: \n\n");
                for (int i = 0; i < numEmps; i++)
                {
                printf("%d) %s \n", i + 1, 
                                    emps[i].emps.name);
                }

                scanf_s("%d", &choice);
                printf("%s \n", emps[choice - 
                                1].emps.name);
                printf("%.2f \n", emps[choice - 
                                1].emps.hours);
                printf("%.2f \n", emps[choice - 
                                1].emps.rate);

                break;

            case 4: PayOutput();
                break;

            case 5: printf("Quitting program!");
                saveEmployee();
                return;

            default: printf("Invalid choice try again \n\n");
                break;
            }
        } while (choice != 5);
}  

int main()
{
    struct information empsi[20];
    loadMenu();
}

最佳答案

你的函数loadEmployee只写入函数本地的char[](这意味着它在最后被丢弃)。

当程序退出时,您可以通过在写入写入模式下重新打开文件来“保存”该员工,这会清除它,而接下来的操作可能没有多大作用,因此文件保持为空。

尝试在函数外部实际返回或存储文件中的数据,以便稍后重用。

关于c - 如何将文本文件传递到c中的数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57351319/

相关文章:

c - 赋值,关于二维数组

java - 将 Array 的元素添加到 ArrayList

java - 如何从 jar 文件中读取文件?

c - C 语言中 inet_aton() 和 gethostbyname() 的区别?

C:代码重复示例

Objective-c数组

c++ - 如何使用数组指针?

c++ - 获取用户计算机上的文件和文件夹列表,文件名由文本行过滤

objective-c - 有没有办法读取 TypedStream 格式的文件

python - 保存和读取加倍且不损失精度