反序列化C(链表)中的文本文件数据



我想让文本文件数据变得可读。到目前为止,我已经成功地序列化了代码,但在从文本文件中反序列化代码并在屏幕上打印数据时遇到了麻烦。

struct Node{
    struct Node *next;
    int c;
    int d;
};
struct Node *head=NULL;
struct Node *current=NULL;
void display(){
    struct Node *temp=head;
    while(temp!=NULL){
        printf("nData : %d", temp->c);
        printf("nData : %d", temp->d);
        temp=temp->next;
    }
}
//Problem here 
struct Node* deserialize(char* data, struct Node *p){
    sscanf(data,"%d %d", &p->c, &p->d );
    return p;
 }
char* serialize(const struct Node *p){
    char* ser = malloc(100*sizeof(*ser));
    if(!ser){
        printf("nUnable to allocate memory");
    }else{
        sprintf(ser, "%d %d",p->c, p->d);
    }
    return ser;
}
// and here
void readFromFile(){
    char *d =malloc(100*sizeof(*d)); 
    FILE *fp =  fopen("read.txt", "r");
    struct Node *f;
    for(f= head; f!=NULL; f=f->next){
       fscanf(fp, "%[^n]%*c", d);
      // printf("nString : %s",d);
         deserialize(d, f); 
    printf("n%d %d", f->c, f->d);
}
fclose(fp);
}
void createNode(){
    struct Node *temp = NULL;
    temp = (struct Node* )malloc(sizeof(struct Node));
    if(temp == NULL){
        printf("nttMemory for the new record cannot be allocated!!!");
        return;
     }
     printf("nEnter number");
     scanf("%d",&temp->c);
     printf("nEnter number");
     scanf("%d",&temp->d);
     temp->next = NULL;
        if(head==NULL){
        head = current = temp;
    }else{
        current->next = temp;
        current = temp;
    }
     printf("Record Inserted");
        char* data = serialize(temp);
        FILE *fp =  fopen("reads.txt", "a");
        fprintf(fp, "%sn",data);
        fclose(fp);
        free(data);
}
int main(){
    int i;
    for(i=0; i<3; i++){
        createNode();
    }
    display();
    readFromFile();
}

我在fscanf和sscanf部分遇到问题。我怎样才能把它做好?

struct Node* deserialize(char* data, struct Node *p){
    sscanf(data,"%d %d", p->c, p->d );
    return p;
 }

应该是

struct Node* deserialize(char* data, struct Node *p){
    sscanf(data,"%d %d", &(p->c), &(p->d) );
    return p;
 }
#include <stdlib.h>
#include <stdio.h>

struct Node{
    struct Node *next;
    int c;
    int d;
};
struct Node *head=NULL;
struct Node *current=NULL;
struct Node* newNode(int c,int d){
    struct Node*  nd;
    nd=malloc(sizeof(*nd));
    nd->c=c;
    nd->d=d;
    nd->next=NULL;
    return nd;
}
struct Node* AppendNode(struct Node*parent,struct Node* nd){
    if(parent)
        parent->next=nd;
    return nd;
}

void display(){
    struct Node *temp=head;
    while(temp){
        printf("Data : %d, %dn", temp->c, temp->d);
        temp=temp->next;
    }
}
//Problem here 
struct Node* deserialize(char* data, struct Node *p){
    sscanf(data,"%d %d", &p->c, &p->d );
    return p;
 }
char* serialize(const struct Node *p){
    static char ser[100];
    sprintf(ser, "%d %d",p->c, p->d);
    return ser;
}
// and here

void readFromFile(const char *filename){
    struct Node     *f=NULL;
    int             c,d;
    FILE *fp =  fopen(filename, "r");
    while(fscanf(fp, "%d %d", &c,&d)==2){
        f=AppendNode(f,newNode(c,d));
        if(!f){
            perror("readFromFile::AppendNode()n");
            exit(-1);
        }
        if(!head)
            head=f;
    }
    fclose(fp);
    return ;
}
void createNode(const char *filename){
    struct Node *temp=newNode(0,0);
    if(!temp){
        perror("createNode::newNode()n");
        exit(-1);
    }
    printf("Enter number: ");
    scanf(" %d",    &temp->c);
    printf("Enter number: ");
    scanf(" %d",    &temp->d);

    current=AppendNode(current,temp);
    if(!head) head=current;
    printf("Record Insertednn");

    FILE *fp =  fopen(filename, "a");
    fprintf(fp, "%sn",serialize(temp));
    fclose(fp);
}
void DeleteNodes(){
    while(head){
        current=head;
        head=head->next;
        free(current);
    }
}
#define DATA_FILE   "read.txt"
int main(){
    int i;
    for(i=0; i<3; i++){
        createNode(DATA_FILE );
    }
    printf("printing entries: n");
    display();
    DeleteNodes();
    printf("nreading from file: n");
    readFromFile(DATA_FILE );
    display();
    DeleteNodes();
    printf("nDONE: n");
    return 0;
}

相关内容

  • 没有找到相关文章

最新更新