Hey.

I've written a bit of c++ code to move columns of data from similarly named files (file20.txt, file40.txt, file60.txt etc) into a single file. Unfortunately there seems to be at least 1-2 errors (which I cannot seem to fix :S).

I'm trying to take data from a list of files with names from 50ms.txt-1050ms.txt with a stepping size of 20 in the filename but for some reason an error occurs at the "filename.replace..."-line when replacing 90 with 110 - a bit stranger is that this error doesn't occur every time when I run the compiled program in Windows (compiled in Dev C++) whereas it does in Ubuntu (using g++). Furthermore the Dev C++ compiled program has an error located around "return 0;" (which I don't understand either).

Here's the full code:

/*A small program to copy data columns from several similarily named files into a single sample file. */

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

int main(){
  int i, j, k;
  int ncol, nrow, col, nfiles;
  int fstart, fstep, fend;
  long double **x;
  string filename, dump, strnumber, str="*";
  stringstream ss;
    
  cout << "Insert the name of the input file: ";
  cin >> filename;
    
  cout << "Insert number of first file, step-size and number of last file: ";
  cin >> fstart >> fstep >> fend;
    
  nfiles = (fend-fstart)/fstep;
  cout << nfiles+1 << " will be opened.\n";
    
  cout << "Insert number of columns and rows: ";
  cin >> ncol >> nrow;
  ncol--;                                //makes it easier to make ncol loops (i.e. from 1->ncol/0->ncol-1)
  nrow--;
    
  x = new long double*[nrow];
    
  for (j=0;nrow+1>=j;j++){
    x[j] = new long double[nfiles];
  }
    
  cout << "Which column do you want to sample? ";
  cin >> col;
  col--;
    
  for (k=0;nfiles>=k;k++){
    if(!(k==0)){
      str = strnumber;
    }
    ss << fstart + fstep*k;
    strnumber = ss.str();
    ss.str(std::string());
    filename.replace(filename.find(str),str.length(),strnumber); //sometimes error when going from 90-110...
    
    ifstream input(filename.c_str());
    
    for (i=0;3>=i;i++){                           //to skip the first 4 lines.
      getline(input, dump);
    }
    
    for (i=0;nrow>=i;i++){
      for (j=0;ncol>=j;j++){
	if(j==col){
	  input >> x[i][k];
	}
	else{
	  input >> dump;
	}
      }
    }
    
    input.close();
  }
    
  ofstream output ("SamplerOutput.txt");
    
  for (i=0;nrow>=i;i++){
    for (k=0;nfiles>=k;k++){
      output << x[i][k] << "\t";
    }
    output << "\n";
    cout << i << "\t";
  }
    
  output.close();
    
  return 0; //for some reason this returns an error...?
}
Comments
Excellent 1st post
Phasespace
Deleted Member

Do you have a sample input file to post?

And sample input from the command prompt etc.

Oh, right, I totally forgot... you can find the source code and a number of sample input files at www.phasespace.dk/Sampler.rar and the input I gave via the command prompt was
*ms.txt 5 5 395 3 41 2

i.e. telling the program to open files with names *ms.txt where * is replaced by 5, 10, 15,..., 395, that the data is written in 3 columns x 41 rows and that I'm interested in column 2.

This article has been dead for over six months. Start a new discussion instead.