嗯...我是 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/