save memory to disk

P

pfemat

Hi there,

i am doing some heavy computations whose result i want to save
to harddisk. the results are in an long[nsize] array named screen
and i thought of doing

char *tmp = reinterpret_cast<char*>(screen);

for (int i=0; i<nsize; i++)
{
datafile << tmp << '\n';
}

for saving onto disk and

char* tmp = new char[nsize];

for (int i=0; i<nsize; i++)
{
datafile >> screen;
}

screen = reinterpret_cast<long*>(tmp);

afterwards i compare the screen array with an screen2 array where
the latter is filled with the data from disk. and get

screen!=screen2;

for most i.

i am reconstructing the case from memory since i worked onwards
with a workaround.

thanks in advance for any hints or comments.

Matthias
 
S

Steven Hudson

Why not use the read and write functions.
i.e datafile.write((char *)data_arry, sizeof(data_arry));
 
J

Jack Klein

Hi there,

i am doing some heavy computations whose result i want to save
to harddisk. the results are in an long[nsize] array named screen
and i thought of doing

Do you mean your data is actually an array of type long (i.e., short
hand for "signed long integer")? In that case, why not just write a
loop that inserts nsize longs?
char *tmp = reinterpret_cast<char*>(screen);

for (int i=0; i<nsize; i++)
{
datafile << tmp << '\n';
}

for saving onto disk and

char* tmp = new char[nsize];

for (int i=0; i<nsize; i++)
{
datafile >> screen;
}

screen = reinterpret_cast<long*>(tmp);


Yes, it appears that your data really is "signed long int". So you
have quite a few problems. First, the size of the array "long screen
[nsize]" is equal to nzize * sizeof(long) bytes, but you are writing
only nsize * 1 bytes, because sizeof(char) is 1 by definition. I am
willing to bet that yours is not one of the few platforms where
sizeof(long) == sizeof(char), and in fact on your platform
sizeof(long) is probably 4. So if nothing else is wrong, you would
only write 1/4 of your data to the file.
afterwards i compare the screen array with an screen2 array where
the latter is filled with the data from disk. and get

screen!=screen2;

for most i.

i am reconstructing the case from memory since i worked onwards
with a workaround.

thanks in advance for any hints or comments.

Matthias


What exactly is the data? Is it literally printable characters, like
one long character string? If the data is binary, are you opening the
file in binary mode? You need to do this and also to use the .read()
and .write() functions for binary i/o.

But I think you just need to leave out the static_cast and loop
through your array using << to output longs as longs. Why do you
think you want to read and write them as characters?
 
P

perry

you know, i think somewhere along the line, people forgot that C & C++
were origionally designed with the developement of operating systems in
mind. application programs can so much better be addressed with lanuages
that do not require such "performance" issues....

sure Java, is an ideal solution, but it's not the only one. the problem
with using C++ for everything that moves is that you always reinventing
the wheel....

just my two cents worth for today

- perry

Jack said:
Hi there,

i am doing some heavy computations whose result i want to save
to harddisk. the results are in an long[nsize] array named screen
and i thought of doing


Do you mean your data is actually an array of type long (i.e., short
hand for "signed long integer")? In that case, why not just write a
loop that inserts nsize longs?

char *tmp = reinterpret_cast<char*>(screen);

for (int i=0; i<nsize; i++)
{
datafile << tmp << '\n';
}

for saving onto disk and

char* tmp = new char[nsize];

for (int i=0; i<nsize; i++)
{
datafile >> screen;
}

screen = reinterpret_cast<long*>(tmp);



Yes, it appears that your data really is "signed long int". So you
have quite a few problems. First, the size of the array "long screen
[nsize]" is equal to nzize * sizeof(long) bytes, but you are writing
only nsize * 1 bytes, because sizeof(char) is 1 by definition. I am
willing to bet that yours is not one of the few platforms where
sizeof(long) == sizeof(char), and in fact on your platform
sizeof(long) is probably 4. So if nothing else is wrong, you would
only write 1/4 of your data to the file.

afterwards i compare the screen array with an screen2 array where
the latter is filled with the data from disk. and get

screen!=screen2;

for most i.

i am reconstructing the case from memory since i worked onwards
with a workaround.

thanks in advance for any hints or comments.

Matthias



What exactly is the data? Is it literally printable characters, like
one long character string? If the data is binary, are you opening the
file in binary mode? You need to do this and also to use the .read()
and .write() functions for binary i/o.

But I think you just need to leave out the static_cast and loop
through your array using << to output longs as longs. Why do you
think you want to read and write them as characters?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top