Text file i/o

Hi, I have written the following code to add data to text files which are
required to store 3D scan data (I have to calculate the y-coordinate). My
code seems to work except that it stops working when I want to create more than
ten text files i.e. the directory I am trying to store them in will not hold any more than ten text files. Any ideas why this might be happening? Code is shown below. Thanks.

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

int main()
{
// Declare required variables
int prs, tpr, fs, j=0, k=0;
double inc;
string file, file1, line,dir, s, s1;

// Prompt user for necessary scan information
cout << "Enter Feed Speed: ";
cin >> fs;
cout << "Enter No. of Profiles/s: ";
cin >> prs;
cout << "Enter Total No. of Profiles: ";
cin >> tpr;
cout << "Provide name of directory: ";
cin >> dir;

// Calculates increment in y-direction
inc = fs / (float)prs;
// Initialise y coordinate to -ve increment
double y = -inc;

for (int i = 0; i < tpr; i++)
{
// y co-ordinate increments
y += inc;
// increment file number (j)
j += 1;
// increment input file number (k)


// Converts coordinate to a string for insertion to *.txt file
stringstream converty, convertj;
converty << y;
convertj << j;

// Creates output file stream object called "outFile" and prompts user to provide a name
// Changes output filename with each successive increment of j

s = convertj.str();
char oname[] = "profile_00x.txt";
oname[10] = s[0];
file = dir + "/" + oname;

double column1[] = {-37.43, -38.465, -33.23, -36.75, -37.45};
double column2[] = {150.26, 151.35, 152.86, 156.75, 153.83};

ofstream outFile (file.c_str(), ios::out);
for (int k= 0; k < 5; k++)
{
outFile << column1[k] << "\t";
outFile << converty.str() << "\t";
outFile << column2[k] << endl;
}

outFile.close();

}

return 0;
}
Use the std::string mechanism:
file = dir + "/profile_00" + convertj.str() + ".txt";

you can also use the stringstream for operations like that.
Topic archived. No new replies allowed.