Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8026051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:21:41+00:00 2026-06-04T23:21:41+00:00

I’m doing a simulation of a few Ising models (lattice with 1 or -1)

  • 0

I’m doing a simulation of a few Ising models (lattice with 1 or -1) and I don’t want to use files (don’t gonna get into details here, I guess :D). So, the thing is, I’m using stringstreams to format the data & data’s filename to then separate them with bash. Show you an example code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>    
#include <iostream>
#include <sstream>
#include <string>    
using namespace std;
void print2bash(string file, string data);

int main(int argc, char *argv[]){

    int i, j, n = atoi(argv[1]);
    // I want to put this outside main() {
    stringstream ssbash;    ssbash.clear(); ssbash.str(""); 
    stringstream ssfile;    ssfile.clear(); ssfile.str("");
    //} 
    //  So I don't have to do ss.clear and ss.str("") every time before using them

    ssfile << "this_" << n << "_is_the_filename.dat" << endl;
    for(i = 0; i < n ; i++){
        for(j = 0; j < n; j++)
            ssbash << i*n+j << "\t";
        ssbash << endl;
    }

    print2bash( ssfile.str(), ssbash.str() );

    return 0;
}
// print2bash(); prints string data; to string file; 
//               This will be used to separate data 
//               afterwards ( i.e. ./Ising.o <arguments> | bash )  
void print2bash(string file, string data){

    stringstream ssbash;    ssbash.clear(); ssbash.str(data);       
    stringstream output;    output.clear(); output.str("");
    string line;

    output << "for((i=0;i<1;i++)) do "<< endl; // Buffer all to bash at once
    while( getline(ssbash,line) )
        output << "    echo \"" << line << "\";" << endl;

    output << "done >> " << file; // Appending
    cout << output.str() << endl;

    ssbash.clear(); ssbash.str("");

    return ;
}

A regular output of this “sstreams.o” program would be

~work/Ising$ ./sstreams.o 5
for((i=0;i<1;i++)) do 
    echo "0 1 2 3 4 ";
    echo "5 6 7 8 9 ";
    echo "10 11 12 13 14 ";
    echo "15 16 17 18 19 ";
    echo "20 21 22 23 24 ";
done >> this_5_is_the_filename.dat

You get the idea right? From this thinking flow, what I actually write is ./sstreams.o 10 | bash so one gets nicely separated files. But the problem is:

I’m getting tired of writing this lines each time I want to use print2bash() (oh the irony!)

stringstream ssbash;    ssbash.clear(); ssbash.str(""); 
stringstream ssfile;    ssfile.clear(); ssfile.str("");

Because of that, I want to put the ss as global variables (don’t know if this is that kind of awful so the compiler doesn’t let me to do it: I’m just a Physicist). So, even if I write std::stringstream for the ss variables after the #includes (and using namespace std), I still get the error

error: ‘ssbash’ does not name a type
error: ‘ssbash’ does not name a type
error: ‘ssfile’ does not name a type
error: ‘ssfile’ does not name a type

from the compiler. Hope you can help meee.


EDIT: [SOLVED]

The problem was that if I wanted to declare a stringstream out of main() it failed because of the members clear & str where out of context so they can’t be used. Besides that, this use was unnecessary because stringstreams are created with empty contents, as @jpalecek and @umlaeute pointed out.

I’ve declared the stringstreams out of main and include a clear() + str("") on print2bash() to give the desired result:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>    
#include <iostream>
#include <sstream>
#include <string> 
#include <vector>

using namespace std;

stringstream ssbash, ssfile;

void print2bash(string file, string data);

int main(int argc, char *argv[]){

    int i, j, n = atoi(argv[1]), p, np = atoi(argv[2]);
    vector< vector<int> > chain;    chain.resize(n*n);

    for( p = 0; p < np; p++){

        ssfile << "chain_p="<< p << "_filename.dat" << endl;
        for(i = 0; i < n ; i++){
            for(j = 0; j < n; j++){

                chain[p].push_back( i*n + j );
                ssbash << i*n+j << "\t";
            }
            ssbash << endl;
       }
       print2bash( ssfile.str(), ssbash.str() );
    }

    return 0;
}
void print2bash(string file, string data){

    ssbash.str(data);
    stringstream output;
    string line;

    output << "for((i=0;i<1;i++)) do "<< endl; // Buffer all to bash at once
    while( getline(ssbash,line) )
       output << "    echo \"" << line << "\";" << endl;

    output << "done >> " << file; // Appending 
    cout << output.str() << endl;

    ssfile.clear(); ssfile.str("");
    ssbash.clear(); ssbash.str("");

    return ;
}

a regular output of ./sstreams.o

~/work/Ising$ ./sstreams.o 4 2
for((i=0;i<1;i++)) do 
    echo "0 1   2   3   ";
    echo "4 5   6   7   ";
    echo "8 9   10  11  ";
    echo "12    13  14  15  ";
done >> chain_p=0_filename.dat

for((i=0;i<1;i++)) do 
    echo "0 1   2   3   ";
    echo "4 5   6   7   ";
    echo "8 9   10  11  ";
    echo "12    13  14  15  ";
done >> chain_p=1_filename.dat

Thanks for everything

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:21:43+00:00Added an answer on June 4, 2026 at 11:21 pm

    You needn’t call

    ssbash.clear(); ssbash.str("");
    

    because stringstream objects are automatically created with empty contents. That leaves you with

    stringstream ssbash;
    stringstream ssfile;
    

    That could be a global declaration, but don’t do it, because it won’t help you. You would have to clear it each time you use it.

    If you don’t like repeating the declaration over and over, you can substitute them with a macro

    #define DECLARE_STREAMS \
      stringstream ssbash; \
      stringstream ssfile;
    

    or better

    #define DECLARE_STREAMS(a, b) \
      stringstream a; \
      stringstream b;
    

    used like this: DECLARE_STREAMS(ssbash, ssfile).

    But the best (and cleanest) solution would be to refactor code that formats the data into a single function. That way, you wouldn’t have to worry about repeating the stream declaration.

    BTW why do you instead of writing to a file with C++ emit script that does the writing is beyond me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I am trying to understand how to use SyndicationItem to display feed which is
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.