How do I write a loop that has an unkown end?

I have been programming part time for about a month now so if you reply and I dont understand , please bare with me! I have written a program that will read a text file , grab some lines , do some maths and spit out what it needs to, it loads the text files in sequential order.

Example: file1.txt, file2.txt, file3.txt, file4.txt,

I fstream the first file grab the data I need sling it into multiple variables , close the text file and open the next.

This is done in a for loop.

1234

for (int i = 0, i < 4, i++
{
all my code
}

This works great and im very happy with it, but obviously I only have 4 files here so the loop grabs all 4 and its done. What happens when I dont know how many files I have? say I have 1,000 files my code inside the loop will be fine as im using arrays to store each loop of data but I dont know how to write a loop that says keep looping until it finds the end (either by date of the file or finding no great file number?) or just to keep looping until asked to stop ?

This is all written in console just for me to work out the logic before I put it into UI.

I don't think there is any simple way to do it if you don't know how many files there are, or what they are called. If they are all in the same folder, and only those files are in the folder, you can do it like this (though it is complicated, and requires you to be running Windows):

If you want to get the filename (for example) of the file you just found, the filename of the currently stored file is in FindFileData.cFileName.

If you're not running windows, this won't work, so you may as well not bother trying. If you are, and you have no idea what's going on here, then either you can look it up on MSDN or you can ask someone.

Hmm thanks for the response and I will try that as a failsafe if I cant get my loop working. I know what part of the files are called. All text files are named:

HH20130717Txxxxxxxxx.txt

The first part is just HH(date) that obviously changes daily and the Txxxxxxxxx part is just a random number that always increases (but not by any particular number).

So essentially I am just renaming them to HH1,HH2,HH3,HH4 for testing. but I want to be able to grab the filename of HH(todays date) read in the Txxxxxxxxx part too and loop until it cannot find a bigger number.

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.

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.

Yea that works good! thanks :) , but.. due to the naming convention of the text files I realised it will be way quicker just to load everything in the folder (or from a certain date within the folder) I think that will be easy enough but I am not sure on how I would be able to keep adding new ones so say for example I had button on the UI called "start"

it grabs all the info from the text files and stores them in their respective arrays, but 10 mins later a new file is put into the folder I want that file to be calculated too without hitting "start" again, any ideas?

You're using only C++ and the console interface, right? Or do you have a graphical user interface like SFML?
What you're asking is possible and somewhat expected in Windows API, but I'm skeptical that it would be possible in an efficient manner for the regular console.

I'm assuming that the file is being placed in the folder by the user, so you could give them a refresh option that basically checks the folder for any new content... or before the program finishes do a final check for any new additions to that folder.

OK, so if that's your plan, then I suggest making everything into functions, that way they will be easy enough to copy and past into a new code source. Right now you're going to have to settle for not being able to do everything because the console has its limits like not being able to automatically update itself.
When you start using a GUI you will be able to start putting those functions that you make in console programming into use as a larger program, but for now just try to make the bare-bones version.