方法不写入文件

问题描述 投票:0回答:1

我想在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);
}
c file printf writetofile
1个回答
1
投票

尝试。

f=fopen("employeeData.txt", "w");
© www.soinside.com 2019 - 2024. All rights reserved.