C-读取文件的写入通用方法

时间:2019-07-05 15:47:50

标签: c windows

我该如何编写一个函数

我做了一个写文件的通用方法。

/* Generic function to write array elements to text file */

void saveToFile(const char *fname, const void *a, int n, size_t width, void(*fptr)(FILE *fp, const void *)) {
    FILE *fp = fopen(fname, "wt");
    char *p = (char*)a;
    if (fp == NULL) {
        printf("\nCan not open the file!\n");
        exit(1);
    }

    for (int i = 0; i < n; i++) {
        fptr(fp, p);
        p += width;
    }
    fclose(fp);
}

如何将其更改为可读?

1 个答案:

答案 0 :(得分:1)

假设您现在要创建一个函数来从刚刚写入的文件中读取内容,以下是来自Google快速搜索的approach。另外,对于您想知道的人,“ wt”或“ rt”是指示将以文本模式(默认设置)打开文件的指示符(至少在python中?对C语言不是很确定)。它们不是必需的,建议您使用here中的标准打开标志。

相关问题