Error while creating notepad files.

I wanted to write a program where I can create text files with consecutive numbers, Following is my code for creating text files with the names "1.txt , 2.txt... to 10.txt"
but all I am getting are files with 0.txt 00.txt , 000.txt etc.
Can Anyone help with this ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  #include <iostream>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;

int main()
{
   ofstream outFile;
   string FileName;
   int fileName=0;
   stringstream ss;
   string FileName_1;

   for(int i=1;i<10;i++)
   {
        ss<<fileName;
        FileName=ss.str();
        FileName_1=FileName+".txt";
   outFile.open(FileName_1);
   if(outFile.is_open())
   {
       outFile<<"Hi there";
       outFile.close();
   }
   else
    {
    cout<<"Could not open the file"<<endl;
   }
   }
    return 0;
}
Line 17: fileName is always 0, hence you get only 0. You need to use i and ss needs to be a local variable within the loop. Otherwise the number is added.

To get that filename you can use to_string(...):

http://www.cplusplus.com/reference/string/to_string/?kw=to_string

outFile.open(std::to_string(i)+".txt");
Last edited on
Thanks man ..

It worked :) .

I think my approach was wrong.
I prefer to have filenames with leading zeros, e.g 09 rather than just 9, this makes sorting them into filename sequence more convenient (otherwise you get 1 and 10 together, then 2, 3, 4 etc.

My code, generates names such as "Test05.txt".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

string make_name(const string & label, int n)
{
    ostringstream oss;
    oss << label << setfill('0') << setw(2) << n << ".txt";
    return oss.str();
}

int main()
{
    
    for (int i=1; i<=10; i++)
    {
        string filename = make_name("Test", i);
        ofstream outfile( filename);

        if (outfile)
        {
            outfile << "Hi there" << ", this is file " << filename; 
        }
        else
        {
            cout << "Could not open output file: " << filename << '\n';
        }
    }
    
}
otherwise you get 1 and 10 together, then 2, 3, 4 etc.

Only if you have unintelligent sorting behaviour.
Does Windows still do that?
Linux GUIs usually sort intelligently.
And in the terminal, the -v flag to ls sorts numeric substrings properly.

BTW, your make_name function would be more generally useful if it took a third parameter to pass to setw().
yes, windows still sorts it as text strings.
I saw a 100 line batch file online to resolve it, so its fixable using only the shell, but its ugly.
the gui after (winxp? 2000? unclear?) some point sorts them smart like unix but the shell seems to lack the ability. You can of course make a micro C program to do it or use a windows compatible ls tool.

Last edited on
Regarding file-name sequencing, I wasn't thinking of the OS, rather of various pieces of software such as image browsers or audio file processors, which may have idiosyncrasies of their own. Using a fixed-width for sequence numbers tends to work more predictably, without having to go to extra effort later.
Topic archived. No new replies allowed.