How to make stringstream that reads file content from a memory

Discussion in 'C++' started by Laco, Oct 20, 2006.

  1. Laco

    Laco Guest

    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.
    Laco, Oct 20, 2006
    #1
    1. Advertising

  2. Laco

    mlimber Guest

    Laco wrote:
    > 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
    mlimber, Oct 20, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Van Overbeke
    Replies:
    2
    Views:
    569
    Robrecht Jacques
    Jul 11, 2003
  2. hazz
    Replies:
    6
    Views:
    49,435
    SkyUCHC
    Jun 9, 2010
  3. Vikas
    Replies:
    0
    Views:
    821
    Vikas
    Jul 15, 2003
  4. Ellarco
    Replies:
    5
    Views:
    4,511
    Ellarco
    Sep 24, 2003
  5. Eric
    Replies:
    0
    Views:
    305
Loading...

Share This Page