How to make stringstream that reads file content from a memory

L

Laco

Dear All,

I have some C++ code which is autogenerated and reads data from a file
(numeric values). This external data file "Weights.asc" is not big
(11kb).

The rest of the code is expecting a passed FILE* pointer in order to
read properly the numeric values and compute stuff. This is real-time
application and thus reading / writing files is strongly prohibited and
time expensive.

The data I have is of the correct type but it is in memory. Is there a
way I can create a FILE structure and point it to the data in memory,
or generally convert my in memory data to a FILE structure?

Here is a part of the code:

FILE *loadStream = fopen("Weights.asc","r");

// Get the file version number
weightFileVersion = getWeightFileVersion(loadStream);

// Load Normalization Coefficients
inputFile.loadWeights(seekComponent(loadStream, "File",
"inputFile"),weightFileVersion);

ComputedOutput.loadWeights(seekComponent loadStream, "DataStorage",
"ComputedOutput"),weightFileVersion);

// Load A Weights
inputAxon.loadWeights(seekComponent(loadStream, "Axon",
"inputAxon"),weightFileVersion);
hidden1Axon.loadWeights(seekComponent(loadStream, "TanhAxon",
"hidden1Axon"),weightFileVersion);
outputAxon.loadWeights(seekComponent(loadStream, "LinearAxon",
"outputAxon"),weightFileVersion);

// Load S Weights
hidden1Synapse.loadWeights(seekComponent loadStream, "FullSynapse",
"hidden1Synapse"),weightFileVersion);
outputSynapse.loadWeights(seekComponent loadStream, "FullSynapse",
"outputSynapse"),weightFileVersion);

fclose(loadStream);

My question is whether I can use STRINGSTREAM to make a string variable
with the complete data in the memory and then use the pointer to the
data to execute the rest of the code.

Thank you for your help.
Laco.
 
M

mlimber

Laco said:
Dear All,

I have some C++ code which is autogenerated and reads data from a file
(numeric values). This external data file "Weights.asc" is not big
(11kb).

The rest of the code is expecting a passed FILE* pointer in order to
read properly the numeric values and compute stuff. This is real-time
application and thus reading / writing files is strongly prohibited and
time expensive.

The data I have is of the correct type but it is in memory. Is there a
way I can create a FILE structure and point it to the data in memory,
or generally convert my in memory data to a FILE structure?

Here is a part of the code:

FILE *loadStream = fopen("Weights.asc","r");

// Get the file version number
weightFileVersion = getWeightFileVersion(loadStream);

// Load Normalization Coefficients
inputFile.loadWeights(seekComponent(loadStream, "File",
"inputFile"),weightFileVersion);

ComputedOutput.loadWeights(seekComponent loadStream, "DataStorage",
"ComputedOutput"),weightFileVersion);

// Load A Weights
inputAxon.loadWeights(seekComponent(loadStream, "Axon",
"inputAxon"),weightFileVersion);
hidden1Axon.loadWeights(seekComponent(loadStream, "TanhAxon",
"hidden1Axon"),weightFileVersion);
outputAxon.loadWeights(seekComponent(loadStream, "LinearAxon",
"outputAxon"),weightFileVersion);

// Load S Weights
hidden1Synapse.loadWeights(seekComponent loadStream, "FullSynapse",
"hidden1Synapse"),weightFileVersion);
outputSynapse.loadWeights(seekComponent loadStream, "FullSynapse",
"outputSynapse"),weightFileVersion);

fclose(loadStream);

My question is whether I can use STRINGSTREAM to make a string variable
with the complete data in the memory and then use the pointer to the
data to execute the rest of the code.

You won't be able to point a FILE* to memory unless you can find some
platform-dependent functions to help you (ask for more info in a forum
for your platform; cf.
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9). Apart
from that, you'll have to rewrite your functions at least a little bit
to make use of a stream (if you do this, you could make it use an
ostream& so that you can use an ostringstream or an fstream or
whatever).

Cheers! --M
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top