首页 > 解决方案 > 方法不写入文件

问题描述

我正在尝试在 C 中创建一个方法,以便可以写入文件,但我不确定它为什么不起作用。我的代码有什么问题吗?

预先感谢您的帮助。

void writeToFile(Employee *employeeRecords, int count) {

    FILE * f = NULL;
    fopen("employeeData.txt", "w");
    if (f == NULL)
    {
        printf("Error opening file!\n");
        exit(1);
    }

    const char *text = "Employee data: \n";
    fputs(text, f);

    for(int l = 0; l < count; l++) {

        fprintf(f, "%c", employeeRecords[l].name);      
        fprintf(f, "%c", employeeRecords[l].surname);
        fprintf(f, "%d", employeeRecords[l].age);
        fprintf(f, "%lf", employeeRecords[l].salary);
    }

    fclose(f);
}

标签: cfileprintfwritetofile

解决方案


尝试:

f=fopen("employeeData.txt", "w");

推荐阅读