Read more .txt files within a configured .txt file

closed account (9h5X216C)
Hi, I'm just learning c++ and really need your help to program as stated by topic.

Basically, I have a text file called 'example.txt', and within this file, there are more .txt filenames (e.g. 'abc.txt', '123.txt').

=========================================
For example: // following are contents within 'example.txt'

//contain grid-areas
abc.txt

//contain results
123.txt
=========================================

How do I go about reading in the contents of 'example.txt' and the program upon reading that there are more filenames, proceed to further access them and read them in?

So far, I know how to read in a file using the following codes. But I'm stuck at how to further read the other .txt files in the main file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <fstream>
#include <iostream>
#include <string>

int main (const int argc, const char **argv)
{
    	std::ifstream file;
    	file.open ("example.txt");

    	if (!file.is_open ())
    	{
        	std::cerr << "Error opening file: " << filename << std::endl;
        	return 1;
    	}
   	std::string line;
    	while (file.good ())
    	{
		std::getline (file, line);
		std::cout << line << std::endl;
    	}

	file.close ();
	return 0;
}



Can anyone help me? Please advise, thank you!!
Last edited on
One way would be to check line after you read it and if it contains a filename.
Depending on what you need to you could read this file like the first one or store the filename in a vector and read after you finished the first file.
Hello justagreenie,

Some FYI that might help.

In line 5 if you are not going to use "argc" and "argv" they do not need to be there. Sometimes compilers may complain about variables being defined and not used.

From line 10 on:
1
2
3
4
5
6
7
8
9
10
11
12
13
if (!file)
{
	std::cerr << "Error opening file: " << filename << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(5));  // Requires header files "chrono" and "thread"
	exit(1);
}

std::string line;  // <--- I like to put this just after the opening brace of main. A personal preference of mine. I think it makes the variables easier to find when they are in one place.

while (std::getline(file, line))
{
	std::cout << line << std::endl;
}

The if statement condition is a shorter way of doing what you did. Also it has the advantage of catching other reasons that could cause the file stream to fail. In the if block I added a line to pause the display before the program ends. This gives you and the user a chance to see te error message before the console window closes. I like to use "exit" in place of "return" as it enhances the point that there is a problem. Any number greater than zero denotes that there is a problem and that because it is not zero it is not a normal exit from the program. Using numbers greater than zero can help track down where the problem is.

The while condition is more often written this way. This way when the file stream fails so does the while condition. Your way could easily print out the last read twice before the while condition realizes the file stream is in a failed state.

Lastly I use these line just before the return in main. For me the console window is separate from the IDE I use and when the program ends the console window closes leaving any last lines displayed unreadable. This keeps the window open:
1
2
3
4
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();


Hope that helps,

Andy
Topic archived. No new replies allowed.