从文件到结构的写入和读取或在c中的反向

时间:2012-04-23 17:40:28

标签: c file-io linked-list

嗯......我是c的新人。 我有个问题。我有2个结构。 1是链表的节点列表,1是链接到节点的结构数组。这是我宣布它的方式。

    typedef struct mhs mhs;
    typedef struct kelas kelas;

struct mhs
{
    char nama[31];
    char nim[16];
    int angkatan;
    float ipk;
};

struct kelas
{
    char kls[13];
    int jml;
    mhs siswa[40];
    kelas *next;
};

kelas = node和mhs = struct数组 我想基于节点制作一个文件。所以,如果我有3个节点,那么我将编写3个不同的文件,里面包含mhs,我也想将node-> kls作为namefile。那可能吗?如果是的话我怎么能这样做?谢谢你的进步。

1 个答案:

答案 0 :(得分:0)

类似

#include <stdio.h>


int printNodesToFile(kelas* node) {
    char filename[100] = {0};

    int idx;
    for(idx = 1; node = node->next, idx++; node != NULL) {
        snprintf(filename, sizeof(filename), "kelas.%d.txt", idx);
        FILE* outFile = fopen(filename, "w");
        if(outFile == NULL) return 1;
        //do stuff
        fclose(outFile);
    }
    return 0;
}