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 #include
s (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 stringstream
s are created with empty contents, as @jpalecek and @umlaeute pointed out.
I've declared the stringstream
s 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