c - 从文件写入和读取结构或在 c 中相反

标签 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 作为名称文件。那可能吗?如果是,我该怎么做?感谢您的预付款。

最佳答案

有点像

#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;
}

关于c - 从文件写入和读取结构或在 c 中相反,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10285456/

相关文章:

c - 用许多元素填充数组

c - 错误 : expected ‘=’ , ‘,’ , ‘;’ , ‘asm’ 或 ‘__attribute__’ 数字常量前

file-io - 使用jmeter将提取的数据写入文件

c - 如何使用 printf 显示完整编写的程序?

c - 在 C 中查找字符串中的字符

android - 我如何以字节 block 播放视频?

bash - 令人尴尬的并行工作流创建了太多的输出文件

c - 为什么删除节点中的位置 0 不起作用?

java - 字符链表 - int indexOf(String)

创建一个包含 BST 给定范围内数字的单向链表