struct ERR
{
int gwang;
};
ERR *Wj;
int x=45;
ofstream out;
out.open("man.me",ios::out | ios::trunc | ios::binary);
Wj = new ERR[x];
for (int index=0;index < x;index++)
Wj[index].gwang = index;
out.write((char*)&Wj,sizeof(ERR)*x);
delete [] Wj;
out.close();
// And then I load it back:
ERR *Wj;
int x =45;
ifstream in;
in.read("man.me",ios::in | ios::binary);
Wj = new ERR[x];
in.read((char*)Wj,sizeof(ERR)*x);
for (int index=0;index<x;index++)
Write_Debug("%d",Wj[index]); // Write the numbers to a log file
in.close();
delete [] Wj;
But the numbers in the log file are all -55435432.
It's probably something stupid that I overlooked, Can anyone help ?
Thanks
Edited by - Tornado on 8/11/00 3:28:55 PM
Dynamic malloc with I/O
I have a problem, I'm trying to write to a file a certain size of a structure and then read it back at the same size.
Here's the code:
Goblineye EntertainmentThe road to success is always under construction
Anyone ? (Please )
Goblineye EntertainmentThe road to success is always under construction
I am not really familiar with the C++ library (I''m in the process of moving from C to C++). However it looks to me like the following line is wrong:
out.write ((char*) &Wj, sizeof (ERR)*x);
you are passing the address of Wj, which is already a pointer. So you are passing a pointer to a pointer. I believe what you mean is:
out.write ((char*) &Wj[0], sizeof (ERR)*x);
That passes the address of the first ERR structure which is probably what should be passed (this is just a guess based on C experience).
I guess this is where most people put a famous quote...
"Everything is funnier with monkey''s" - Unknown
out.write ((char*) &Wj, sizeof (ERR)*x);
you are passing the address of Wj, which is already a pointer. So you are passing a pointer to a pointer. I believe what you mean is:
out.write ((char*) &Wj[0], sizeof (ERR)*x);
That passes the address of the first ERR structure which is probably what should be passed (this is just a guess based on C experience).
I guess this is where most people put a famous quote...
"Everything is funnier with monkey''s" - Unknown
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown
Yes, I believe you are 100% right. Your passing a pointer to a pointer and telling it to write the size of the pointer to memory. However its just going to start writing out all adjacent memory offsets to the disk. Which is probably not the desired effect.
That works well if the structure was:
ERR Wj;
out.write((char*)&Wj,sizeof(ERR)*x);
Which passes it by address, not by double pointers.
Thanks,
CodeMonkey
That works well if the structure was:
ERR Wj;
out.write((char*)&Wj,sizeof(ERR)*x);
Which passes it by address, not by double pointers.
Thanks,
CodeMonkey
CODE Monkey
August 12, 2000 03:28 AM
Still doesn''t work !
I''m losing it here...
Here''s the modified code, what''s the problem ?
struct ERR
{
int gwang;
};
ERR *Wj;
int x=45;
ofstream out;
out.open("man.me",ios::out | ios::trunc | ios::binary);
Wj = new ERR[x];
for (int index=0;index < x;index++)
{
Wj[index].gwang = index;
out.write((char*)&Wj[index],sizeof(ERR));
}
delete [] Wj;
out.close();
// And then I load it back:
ERR *Wj;
int x =45;
ifstream in;
in.read("man.me",ios::in | ios::binary);
Wj = new ERR[x];
for (int index=0;index{
in.read((char*)Wj[index],sizeof(ERR));
Write_Debug("%d",Wj[index]); // Write the numbers to a log file
}
in.close();
delete [] Wj;
I''m losing it here...
Here''s the modified code, what''s the problem ?
struct ERR
{
int gwang;
};
ERR *Wj;
int x=45;
ofstream out;
out.open("man.me",ios::out | ios::trunc | ios::binary);
Wj = new ERR[x];
for (int index=0;index < x;index++)
{
Wj[index].gwang = index;
out.write((char*)&Wj[index],sizeof(ERR));
}
delete [] Wj;
out.close();
// And then I load it back:
ERR *Wj;
int x =45;
ifstream in;
in.read("man.me",ios::in | ios::binary);
Wj = new ERR[x];
for (int index=0;index{
in.read((char*)Wj[index],sizeof(ERR));
Write_Debug("%d",Wj[index]); // Write the numbers to a log file
}
in.close();
delete [] Wj;
Still doesn't work !
I'm losing it here...
Here's the modified code, what's the problem ?
Edited by - Tornado on August 12, 2000 4:30:47 AM
I'm losing it here...
Here's the modified code, what's the problem ?
struct ERR{ int gwang;};ERR *Wj;int x=45;ofstream out;out.open("man.me",ios::out | ios::trunc | ios::binary);Wj = new ERR[x];for (int index=0;index < x;index++){ Wj[index].gwang = index; out.write((char*)&Wj[index],sizeof(ERR));}delete [] Wj;out.close();// And then I load it back:ERR *Wj;int x =45;ifstream in;in.read("man.me",ios::in | ios::binary);Wj = new ERR[x];for (int index=0;index<x;index++){ in.read((char*)&Wj[index],sizeof(ERR)); Write_Debug("%d",Wj[index]); // Write the numbers to a log file}in.close();delete [] Wj;
Edited by - Tornado on August 12, 2000 4:30:47 AM
Goblineye EntertainmentThe road to success is always under construction
August 12, 2000 03:45 AM
I don''t think you''re supposed to pass the address of an array index there (I believe it is one). Therefore, change
out.write((char*)&Wj[index],sizeof(ERR));
To this:
out.write((char*)Wj[index],sizeof(ERR));
(I could be wrong, it''s just a guess.)
out.write((char*)&Wj[index],sizeof(ERR));
To this:
out.write((char*)Wj[index],sizeof(ERR));
(I could be wrong, it''s just a guess.)
quote: Original post by Anonymous Poster
I don''t think you''re supposed to pass the address of an array index there (I believe it is one). Therefore, change
out.write((char*)&Wj[index],sizeof(ERR));
To this:
out.write((char*)Wj[index],sizeof(ERR));
(I could be wrong, it''s just a guess.)
First of all, sorry for the multiple posts
Second, I tried converting &Wj[index] to Wj[index] and got a nice little linker error (The cast from Wj[Index] to char* was impossible).
Thanks anyway
Any ideas ?
The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
Anyone have an idea ?
I''m pretty desperate here...
The road to success is always under construction
I''m pretty desperate here...
The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
August 12, 2000 09:20 PM
I tested your code, and replaced your "Write_Debug" function with standard C file I/O (FILE, fprintf). The results in the "log" file looked like this:
0, 6, 2 .. 44.
This leads me to believe that perhaps the error is in your "Write_Debug" function, not the code you posted.
0, 6, 2 .. 44.
This leads me to believe that perhaps the error is in your "Write_Debug" function, not the code you posted.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement