#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>

#include <sstream>
#include <cstring>
using namespace std;

int main(int argc, char *argv[])
{
    fstream filestr;
	for (int i=1; i<=17771;i++)

  filestr.open ("mv_0000001.txt", fstream::in | fstream::out | fstream::app);

  filestr.close();
  system("PAUSE");
   return EXIT_SUCCESS;
}

guys i m trying to create multiple text files to the designated folder using fstream...i have managed to create one file at a time...but i want it to create multiple files for eg - from mv_0000001 ...mv_0000002..mv_0000003......to mv_017771...if u guys can help me out i would appreciate it..thanks a lot

#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>

#include <sstream>
#include <cstring>
using namespace std;

int main(int argc, char *argv[])
{
    fstream filestr;
	for (int i=1; i<=17771;i++)

  filestr.open ("mv_0000001.txt", fstream::in | fstream::out | fstream::app);

  filestr.close();
  system("PAUSE");
   return EXIT_SUCCESS;
}

guys i m trying to create multiple text files to the designated folder using fstream...i have managed to create one file at a time...but i want it to create multiple files for eg - from mv_0000001 ...mv_0000002..mv_0000003......to mv_017771...if u guys can help me out i would appreciate it..thanks a lot

To use the counter number "i" inside your for loop as part of the text file name, you'd have to convert the int to a string so it can be part of the name.

The way I would do it, is using stringstreams. stringstreams are nice because they allow you to manipulate variables as if it were part of "cout" (stream). This will help us because you can "cout" an int, followed by a string, etc, etc. stringstreams have this ability as well.

Here's how you would implement this in your code:

#include <cstdlib>
#include <iostream>
#include <iomanip> // needed to set the width of the number to 7
#include <string>
#include <fstream>
#include <sstream> // needed for stringstream's

#include <sstream>
#include <cstring>
using namespace std;

int main(int argc, char *argv[])
{
    fstream filestr;
    string filePath = "/location/of/files"; // I'd specify a location that you want the files to be created at
    for (int i=1; i<=17771;i++) { // missing opening bracket

    stringstream fileName; // create stringstream called "fileName", creating within the loop intentionally as it will reset itself each time the loop starts, rather than continuing to grow larger each loop

    fileName << filePath << "/mv_" << setfill('0') << setw(7) << i << ".txt"; // making up what the fileName (path and file name) are made of (***explained further below)

  filestr.open (fileName.str(), fstream::in | fstream::out | fstream::app); // convert fileName to a string to be usable as a paramater
    filestr.close();
    } // corresponding closing bracket
    system("PAUSE");
    return EXIT_SUCCESS;
}

*** the line fileName << filePath << "/mv_" << setfill('0') << setw(7) << i << ".txt"; has a few things going on. I'll break them down a little bit:
fileName - starts the stream, like cout would. everything after is being inputted into fileName
filePath - the string created above that contains the location of the files
"/mv_" - the beginning of the file names, before the numbers
setfill('0') - setting the fill character to a 0, so when the number is set wider than the number is (say, set to 7, but number is 1) than there will be leading 0's in front of 1 to make up a width of 7
setw(7) - sets the width of the numbers to 7
i - your counting number in the for loop
".txt" - your file extension, like before


There were a few interesting things that I used here, which is why I actually modified your code to help you. Normally I would have tried explaining, but stringstreams I find somewhat hard to understand, especially if not familiar with the idea, so I figured I'd modify the code and use lots of comments to help explain what's going on :)

Hope this helps!

commented: Very nice :) +20

thanks man...i will work on it n am sure it will help me in some way or the other..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.