NEW PROBLEM:
I'm new to programming a shell script. In fact I just started reading on it, heh! I have a huge list of files, and I'm looking to write a shell script that'll execute one line for each file in the directory.
So...let's say I have a folder with file names such as 101010.ctl, 101011.ctl, 101012.ctl and I want to run the command
mpb 101010.ctl>&101010.out
How do i get the shell script to step through the files in the directory executing that command with the particular filename.
SECOND PROBLEM SOLVED:string itos(int i) // convert int to string
{
stringstream s;
s << i;
return s.str();
}
Hey everyone,
I'm working on a program to write files that I need to run calculations on.
Basically the user will enter values called
center
x
y
I'm going to run some code, but basically I want it to output a file with the name
center + x + y.ctl
So say our center was 10, our x was 20, and our y was 30-32:
102030.ctl
102031.ctl
102032.ctl
Usually if it was just a string, I could use something like
outData.open(output.c_str() );
where the user specified output.
Anyone point me in the right direction? Or what command I should be looking at? It's been about 3 years since i've had C++.
So basically i'm looking for something like
outData.open(centerxy.ctl);
Posts
string filename = center +x + y+".ctl";
outstream.open(filename);
int center = 10878, x = 27897, y = 34;
char d[50];
sprintf(d, "%d%d%d.ctl", center, x, y);
ofstream fout(d);
fout << "hello world" << std::endl;
sprintf can also be used for floats and a bunch of other types by changing the %d.
Well, you said second problem solved, so I'm not sure if you meant this one or the old one which was now the second in the post.
Anyway, like so.
Well, that'll end you up with, say 101010.ctl.out, but you can run it through the cut command to grab just the name without the extension if you want.
basename somefile.ext .ext # returns "somefile"
If you really want to be insane:
1. Get a newline-separated list of all the files in the directory that you want. Use ls and pipe it into a file (might have to set --color=no depending on how you have your terminal set up.)
2. Process this list with sed. Do this: cat filenamelist | sed -e 's/\(^[a-zA-Z0-9_]*\)\.ctl/mpb \1.ctl >& \1.out' > exelist
3. cat exelist... you should have a list of commands in the form "mpb 101010.ctl >& 101010.out", separated by newlines.
4. If so, chmod exelist to 777 and run it.