473,770 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

save memory to disk

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_cas t<char*>(screen );

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

for saving onto disk and

char* tmp = new char[nsize];

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

screen = reinterpret_cas t<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[i]!=screen2[i];

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
Jul 22 '05 #1
3 2518
Why not use the read and write functions.
i.e datafile.write( (char *)data_arry, sizeof(data_arr y));

pf****@web.de wrote:
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_cas t<char*>(screen );

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

for saving onto disk and

char* tmp = new char[nsize];

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

screen = reinterpret_cas t<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[i]!=screen2[i];

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

Jul 22 '05 #2
On 12 May 2004 20:49:02 GMT, pf****@web.de wrote in comp.lang.c++:
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_cas t<char*>(screen );

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

for saving onto disk and

char* tmp = new char[nsize];

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

screen = reinterpret_cas t<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[i]!=screen2[i];

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?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #3
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 "performanc e" 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 Klein wrote:
On 12 May 2004 20:49:02 GMT, pf****@web.de wrote in comp.lang.c++:

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_cas t<char*>(screen );

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

for saving onto disk and

char* tmp = new char[nsize];

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

screen = reinterpret_cas t<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[i]!=screen2[i];

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?


Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
20785
by: Alessandro Monopoli | last post by:
Hi all, I'm searching a PORTABLE way to get the available and total physical memory. Something like "getTotalMemory" and it returns the memory installed on my PC in bytes, and "getAvailableMemory" and it returns the available memory in bytes. Do you know is there's a C function, a c++ Object or anything else that compiles in Linux and Windows to get these data?
3
1449
by: Federico Bari | last post by:
I have to read and encrypted file (with DESCryptoServiceProvider class) and decrypt it without save the clear file in the disk!!! ie i'd like to have the decryption in a string variable because for safe reasons i cannot save the decription in the disk even if for a short time. The CryptoStream seem to work only with in and out files; do somebody know how i can do? Thank you very much. Federico.
15
2419
by: Joe Fallon | last post by:
I would like to know how you can figure out how much memory a given instance of a class is using. For example, if I load a collection class with 10 items it might use 1KB, and if I load it with 1000 items it might use 100KB. How do I measure the amount of memory used once the class is loaded? Thanks! -- Joe Fallon
5
4502
by: Tales Normando | last post by:
The title says it all. Anyone?
2
2782
by: cgd | last post by:
how to do Save Word document to Database in memory, not through the disk
0
904
by: sudhashekhar30 | last post by:
Hi. i have master table on server, which i am storing in xml file at client side. but it is being stored on hard disk, which i don't want. is dere any way to store it in xml format in memory, not on disk.dis should be deleted wen application stops. i am using asp.net2.0(c#). thanks sudha
3
1314
by: MarkTingson | last post by:
hi scripters! is it possible in vb to transfer the table in a databse into an external memory disk? i just want to save ONLY THE TABLE but NOT THE ENTIRE DATABSE in a flashdisk! pls tell me how... i've search the net for codes but i can't find anything.. i hope you will understand me.. this is for our thesis..
10
2312
by: deciacco | last post by:
I'm writing a command line utility to move some files. I'm dealing with thousands of files and I was wondering if anyone had any suggestions. This is what I have currently: $arrayVirtualFile = array( 'filename'=>'filename', 'basename'=>'filename.ext', 'extension'=>'ext', 'size'=>0,
6
4040
by: Jerry Spence1 | last post by:
I am using a network camera that saves the image in a byte array in memory (via their ocx component, and in the OnNewImage event I can get the pointer to the image and the byte length as e.lFrmBytes e.lPtrToImage How do I get at that array in memory and save it to disk? It seems to suggest a pointer which is not supported in VB.
0
9618
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10259
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.