user1279988
user1279988

Reputation: 763

write and read bin file data not right

i define a little complex structure and test the write/read of c++ io.

but may my use of io read and write is not right, so i do not get the result i want, i think may the ifstrream read method i using way is not right, expect somebody help to figure it out.

the LNode structure is like this:

struct LNode
{
    int data;
    LNode *next;
};

the code i use to handle the io is here:

LNode *tmp;
tmp = NULL;

LinkList l = L->next;

ofstream myfile_tom("struct.dat", ios::out | ios::binary);

while ( l ){

    tmp = l;
    cout<<tmp->data<<endl;
    myfile_tom.seekp(0);
    myfile_tom.write((char*)tmp, sizeof (LNode) );

    l = l->next;

}
cout<<"write end\n";


//closing file
myfile_tom.close();

// read and write methods accept a char* pointer

// read code
LNode *y = new LNode[5];

ifstream myfile ("struct.dat", ios::in  | ios::binary |  ios::ate );
myfile.seekg(0);
myfile.read((char*)y, sizeof(LNode) * 5);


(LNode *)y;

// test if read successful
cout<< "test"<< endl;
cout<<"y[0].data"<<y[0].data<<endl;
cout<<"y[1].data"<<y[1].data<<endl;
cout<<"y[2].data"<<y[2].data<<endl;
cout<<"y[3].data"<<y[3].data<<endl;
cout<<"y[4].data"<<y[4].data<<endl;
//

myfile.close();

where L is a LinkList and i have initialize it using "1, 2, 3, 4,5;

typedef LNode *LinkList;

int p;

int status;
status = InitList( L );

for ( p=1; p <= 5; p++) 
    status = ListInsert(L, 1, p);

the all needed definition as background is here:

int InitList(LinkList &L)
{ 
    L = (LinkList)malloc(sizeof(struct LNode)); 
    if(!L) 
        exit(OVERFLOW);
    L->next=NULL; 
    return 1;
}

int ListInsert(LinkList L, int i, int element) {
//
    int j = 0;
    LinkList p = L, s;

    while( p&&j<i-1)
    {
        p = p->next;
        j++;
    }

    if (!p||j>i-1 )
        return 0;

    s = (LinkList)malloc(sizeof(LNode));
    s->data = element;
    s->next = p->next;
    p->next = s;
    return 1;

}

void visit( int c ) // 
{
    printf("%d ",c);
}

int ListTraverse( LinkList L , void (*vi)(int)){

    LinkList p = L->next;
    while ( p ) {
        vi(p->data);
        p = p->next;
    }
    printf("\n");
    return 1;
}

the output of my program is :

after insert at the LinkList L's head insert 1~5:L=
5 4 3 2 1
5
4
3
2
1
write end
test
y[0].data1
y[1].data-842150451
y[2].data-842150451
y[3].data-842150451
y[4].data-842150451

Just the y[0] is right,

so i am consuming.

Upvotes: 0

Views: 146

Answers (1)

Joe Z
Joe Z

Reputation: 17956

Your code stores the entire LNode to the file, and that includes the next pointer. But, pointers are dynamic things, representing current addresses in memory. It makes no sense to store these.

If you're just trying to store the data, and later reconstruct a linked list with the same data, your initial write loop should just write out the data fields and not the next pointers. Later, when you read in the data, you should allocate new structures and establish the next pointers in code, and read the data into the other fields once you've allocated the structures.

All that said, it seems like your code should still function correctly, if for the "wrong reasons." I suspect the real error is this seekp in your loop, which causes you to overwrite the front part of the file over and over:

myfile_tom.seekp(0);

Upvotes: 1

Related Questions