我一直在尝试为作业编写一些代码,它会获取列表的内容并将其保存到文件中。之后,它必须将所述文件中的内容加载到列表中。头文件由我的教授提供。我需要写的是实际的代码。
list.h:
#ifndef LIST_H_
#define LIST_H_
#define TRUE 1
#define FALSE 0
typedef struct nodeR* node;
struct nodeR{
char payload[20];
node next;
node previous;
};
typedef struct listR* list;
struct listR{
node head;
node tail;
int length;
};
list initList(); //creates a new list and returns it. Returns NULL if it fails
int destroyList(list l); //frees memory from the list. Returns TRUE upon success or FALSE upon failure
int getNodeIndex(list l, node targetNode); //returns the position of the targetNode in the list. Returns a negative number if it fails
int getListLength(list l); //returns the length of the list. Returns a negative number if it fails
int addNode(list l, node newNode); //adds newNode at the end of the list. Returns TRUE upon success or FALSE upon failure
int insertNodeBefore(list l, node targetNode, node newNode); //injects newNode in the list right before the targetNode. Returns TRUE upon success or FALSE upon failure
int deleteNode(list l, node targetNode); //deletes targetNode from list. Returns TRUE upon success or FALSE upon failure
list reverseList(list l); //returns a list which is created by reversing the order of the elements of l. Returns NULL if it fails
#endif
file.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "file.h"
#include "list.h"
void saveListToFile(FILE* file, list l){
char name[100];
int i;
node curNode;
printf("Give the name of the file you would like to save the contents of the list to.\n");
fgets(name, 100, stdin);
file = fopen(name, "wb+");
if (file == NULL){
printf("Error: File could not be opened.\n");
return;
}
strcpy(temp, curNode->payload);
curNode = l->head;
while (curNode != NULL){
char temp[20];
strncpy(temp, curNode->payload, sizeof(temp));
fwrite(temp, 1, sizeof(temp), file);
curNode = curNode->next;
}
fclose(file);
}
int getNodesInFile(FILE* file){
char name[100];
int nodes;
fseek(file, 0, SEEK_END);
nodes = ftell(file) / 20;
fseek(file, 0, SEEK_SET);
return nodes;
}
void loadListFromFile(FILE* file, list l){
char name[100];
value[20];
int nodes, i;
node temp;
printf("Give the name of the file you would like to load the contents of on a list.\n");
fgets(name, 100, stdin);
strtok(name, "\n");
file = fopen(name, "r");
if (file == NULL){
printf("Error: File could not be opened.\n");
return;
}
nodes = getNodesInFile(file);
printf("%d\n", nodes);
for (i = 0; i<nodes; i++){
fread(value, sizeof(value), 1, file);
strcpy(temp->payload, value);
addNode(l, temp);
}
fclose(file);
}
main.c中:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "file.h"
#include "list.h"
int main(){
int i;
list l;
l = initList();
node r1, r2, r3, r4, curNode, current;
FILE* file;
// loadListFromFile(file, l);
// printf("a\n");
// current = l->head;
// printf("b\n");
// while (current != NULL){
// printf("%sh\n", current->payload);
// current = current->next;
// }
r1 = malloc(sizeof(struct nodeR));
r2 = malloc(sizeof(struct nodeR));
r3 = malloc(sizeof(struct nodeR));
r4 = malloc(sizeof(struct nodeR));
strcpy(r1->payload, "test1");
strcpy(r2->payload, "test2");
strcpy(r3->payload, "test3");
strcpy(r4->payload, "test4");
addNode(l, r1);
addNode(l, r2);
addNode(l, r3);
addNode(l, r4);
curNode = l->head;
for (i = 0; i < l->length; i++){
printf("%s\n", curNode->payload);
curNode = curNode->next;
}
reverseList(l);
current = l->head;
while (current != NULL){
printf("%s\n", current->payload);
current = current->next;
}
saveListToFile(file, l);
current = l->head;
while (current != NULL){
printf("%s\n", current->payload);
current = current->next;
}
destroyList(l);
}
我尝试使用主程序测试代码,该程序创建4个节点的列表,为每个节点提供“test1-4”有效负载值,然后调用saveListTofile()尝试将列表内容保存到文件中。当我之后打开文件时,其中有一行是[object Object]。我对文件很新,所以我不知道是什么导致了这个问题,到目前为止我所尝试的都失败了。非常感谢任何帮助。
答案 0 :(得分:1)
在保存的功能中,您需要为每个节点计算temp
。
类似的东西:
strcpy(temp, curNode->payload);
for(; curNode != NULL; curNode = curNode->next) {
char temp[20];
strncpy(temp, curNode->payLoad, sizeof temp);
fwrite(temp, 1, sizeof temp, file);
curNode = curNode->next;
}
您还必须以二进制模式("wb+"
)打开文件。
注意:上述内容可能是我第一次在此网站上推荐strncpy()
。对固定宽度记录的这种需求非常罕见。惊人! :)