c语言 - 尝试将双向链表的内容写入文件



我一直在尝试为赋值编写一些代码,该赋值获取列表的内容并将它们保存到文件中。之后,它必须将所述文件中的内容加载到列表中。头文件是我的教授给出的。我需要编写的是实际代码。

列表.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

文件.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("%dn", nodes);
    for (i = 0; i<nodes; i++){  
        fread(value, sizeof(value), 1, file);   
        strcpy(temp->payload, value);
        addNode(l, temp);           
    }
    fclose(file);                   
}

主.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("an");
    //  current = l->head;
    //  printf("bn");
    //  while (current != NULL){
    //      printf("%shn", 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("%sn", curNode->payload);
        curNode = curNode->next;
    }
    reverseList(l);
    current = l->head;
    while (current != NULL){
        printf("%sn", current->payload);
        current = current->next;
    }
    saveListToFile(file, l);
    current = l->head;
    while (current != NULL){
        printf("%sn", current->payload);
        current = current->next;
    }
    destroyList(l);
}

我尝试使用一个主程序测试代码,该程序创建一个包含 4 个节点的列表,为每个节点提供一个"test1-4"有效负载值,然后调用 saveListTofile() 来尝试将列表的内容保存到文件中。当我之后打开文件时,其中只有 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()。这种对固定宽度记录的需求非常罕见。了不起!:)

相关内容

  • 没有找到相关文章

最新更新