• 0

Atomatic naming files


Question

hello every1

i want to write a program that automatically makes files like 1.txt,2.txt,3.txt,... Can any1 help me please? :wacko:

Edited by wildworld_ammsa
Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0
hello every1

i want to write a program that  automatically makes files like 1.txt,2.txt,3.txt,... Can any1 help me please? :wacko:

If you just want to make the files, this code will do:

#include <string>
#include <fstream>
#include <sstream>

std::string numToStr (int n)
{
    std::stringstream ss;
    std::string ret;
    ss << n;
    ss >> ret;
    return ret;
}
int main (int argc, char **argv)
{
    const int startFrom = 0;                 // the number to start from when creating files
    const int maxFiles = 100;                // the last number
    for ( int i = startFrom; i <= maxFiles; ++i )
    {
        std::string tmpFile = numToStr(i) + ".txt";
        std::fstream fStream;
        fStream.open( tmpFile.c_str(), std::ios::out | std::ios::trunc );
        if (fStream.is_open())
            fStream.close();
        fStream.clear();
    }
    return 0;
}

Hope that helps.

Link to comment
Share on other sites

  • 0

Just in case you want to see the C way...

FILE *f;
char szFile[64];
for(int i = 1; i < 5; i++)
{
  sprintf(szFile,"%d.txt",i);
  f = fopen(szFile,"r");
  fclose(f);
}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.