I have a solution for where the numbers might not be in order, but if you know there aren't any skipped numbers then you could set this up to run much quicker, and this program does have a maximum range, but that isn't a bad thing because otherwise you would have an infinite loop which would get you a frozen screen.
I can think of several ways that you could work this out differently, either have all the numbers be sequential (001, 002, 003, etc) <- most efficient, keep a record of the highest number that you reach and use that as the top of your range, save each filename that you create into a master .txt file which you could search through... or use NT3's example...
Delete the cout>>filename that is inside the first loop, it's only to show that the naming scheme is working, but the graphics slow down the run-time.
Also, right now I have the program opening the files using ifstream because it doesn't create a file when none exists, while ofstream does, so what you can do is use this setup to record what numbers on the files actually exist, then reuse those stored numbers when you start with ofstream.
You'll have to rework this code to figure in that part about today's date, I haven't worked in <time.h> or <locale.h> which is where I think you would get the date and time info.
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int incrementstring(string &num);
int main()
{
//for sequential files...
string filename;
string word1 = "File"; //set to whatever date format like "2013.07.18"
string number = "0000"; // this is how you set the max range of numbers to search,
//More 0's means higher search range
string extension = ".txt";
filename = word1;
filename += number;
filename += extension;
ifstream file; //you can use ifstream a a test since it doesn't automatically create a file if it isn't found.
//if you need to use ofstream then record which number got a hit in the if(file.good()) block
while(incrementstring(number))
{
filename = word1;
filename += number;
filename += extension;
file.open(filename);
cout<<filename; //this runs much quicker without a cout in the loop,
//I just want to show that the filename is running correctly.
if(file.good())
{
//File is good, which means it is both opened and exists
}
file.close();
}
int wait;
cin>>wait;
}
int incrementstring(string &num)//returns 0 if the num is at the max, can't be incremented
{
static string hold = num;
int len = num.length();
int count = len-1;
num[count]++;
while(count>0)
{
if(num[count]>'9')
{
num[count] = '0';
num[count-1]++;
}
count--;
}
if(num[0]>'9')
{
num[0] = 'h';
return false;
}
else
return true;
}
| |
I didn't try NT3's code, but since you said you haven't been coding for long I wanted to make sure you had a native C++ example that didn't have any Windows API. Follow NT3's example if you meant you have been dealing with Windows Programing for several months.