我在做两项工作第一个将列表从文件加载到程序中(在开始时),第二个将列表的数据保存到文件中我试过写这两个函数,但我很难(我不是很擅长编程)下面是我使用的结构,我还添加了索引的定义和列表的创建:
#define index 30
typedef struct dataR* data;
struct dataR{
int age;
char name[index];
};
typedef struct nodeR* node;
struct nodeR{
data a;
node next;
};
typedef struct listR* list;
struct listR{
node head, tail, curr;
int size;
};
list list_create(){
list List=malloc(sizeof(struct listR));
assert(List);
List->head=NULL;
List->tail=NULL;
List->curr=NULL;
List->size=0;
return List;
}
下面是将文件数据加载到列表中的函数(在程序开始时)显然这是错误的,但我不知道如何将数据加载到每个节点,因为列表是空的:
list load(char *filename, list List)
{
FILE *fd=fopen("filename","r");
fscanf(fd, "%d",&(List->head->a->age));
fscanf(fd, "%s",&(List->head->a->name[index-1]));
fclose(fd);
fd=NULL;
}
下面是在程序结束时保存列表中所有数据的函数:
void save(char *filename, list List)
{
FILE *fd=fopen("filename.dat","w");
if (fd==NULL)
{
printf("File does not exist");
return;
}
fwrite(List->head->a, sizeof(struct dataR),1,fd);
node tmp=List->head->next;
while(tmp->next!=NULL)
{
fwrite(tmp->next->a, sizeof(struct dataR),1,fd);
tmp=tmp->next;
}
fclose(fd);
fd=NULL;
}
文件中的数据应如下所示:
35 Nick
29 Jim
19 Helen
好吧,当然,函数并没有像现在这样做所以我需要一些帮助来改善他们任何关于档案的提示和帮助都非常感谢。很抱歉给你发了这么长的信谢谢你抽出时间。
最佳答案
像这样修理
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define S_(v) #v
#define S(v) S_(v) //stringify
#define MAX_NAME_LENGTH 30 //index is bad name
typedef struct dataR* data;
struct dataR{
int age;
char name[MAX_NAME_LENGTH + 1];
};
typedef struct nodeR* node;
struct nodeR{
data a;
node next;
};
typedef struct listR* list;
struct listR{
node head, tail, curr;//curr unused?
int size;
};
list list_create(void){
list List = malloc(sizeof(*List));
assert(List);
List->curr = List->tail = List->head = NULL;
List->size=0;
return List;
}
void addList(list List, data new_data){
node new_node = malloc(sizeof(*new_node));
new_node->a = new_data;
new_node->next = NULL;
if(List->head == NULL)
List->head = List->tail = new_node;
else
List->tail = List->tail->next = new_node;
++List->size;
}
list load(const char *filename, list List){
FILE *fd = fopen(filename,"r");//"filename"--> filename, LOL
if(!fd){
fprintf(stderr, "%s can't open in load.\n", filename);
perror("fopen");
return NULL;
}
int age;
while(EOF != fscanf(fd, "%d", &age)){// or 1 == fscanf(fd, "%d", &age)){
data new_data = malloc(sizeof(*new_data));
new_data->age = age;
fscanf(fd, "%" S(MAX_NAME_LENGTH) "s", new_data->name);
addList(List, new_data);
}
fclose(fd);
//fd=NULL;//meaningless
return List;//need return value;
}
void save(const char *filename, list List){
FILE *fd=fopen(filename, "w");
if(!fd){
fprintf(stderr, "%s can't open in save.\n", filename);
perror("fopen");
return ;
}
//fwrite(List->head->a, sizeof(struct dataR),1,fd);//fwrite doesn't match load
node tmp = List->head;
while(tmp){
fprintf(fd, "%d %s\n", tmp->a->age, tmp->a->name);
tmp = tmp->next;
}
fclose(fd);
}
int main(void){
list List = list_create();
load("list.txt", List);
printf("List hold %d data.\n", List->size);
data addData = malloc(sizeof(*addData));
addData->age = 73;
strcpy(addData->name, "Ken");
addList(List, addData);
save("list.txt", List);
//deallocate
return 0;
}
关于c - C:如何从文件加载列表并将其保存在程序末尾,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37314186/