Reading .txt files inside a .txt file

Hi, I am not too experienced with C++ and am currently working on a project that requires reading in a .txt file that contains a list of names of other .txt files, which contain random words and symbols that must be eventually parsed. So far, I have successfully been able to open the initial file using ifstream infile(), but am unsure how to access the files listed inside the initial file. My initial thought was to define a function that stores each .txt file into a vector as a string, but this could be the wrong path. Any tips of where to start or advice would be greatly appreciated. Thank you in advance!
Hello misalo,

Yes using a vector is a good idea and the best way to store the file names.

It is also a good idea to post what code you have written so far.

Along with posting code:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.

Also include the text file that is first opened and read. This everyone will be working with the same information.

Hope that helps,

Andy
I agree with Handy Andy that a vector of string is the best option.
Here's a snippet to get you started:
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 <fstream>
#include <string>
#include <vector>

using namespace std;

vector<string> read_lines(const string& filename)
{
  vector<string> retval;
  ifstream src(filename);
  if(!src)
  {
    // handle error
  }
  string line;
  while(getline(src, line))
    retval.push_back(line);

  return retval;
}

void process_file(const string& fname)
{
  // your code here
}

int main() 
{
  for (const string& filename : read_lines("Your file name"))
  {
    process_file(filename);
  }
}
Hello misalo,

If you can understand Thomas1965's example it is very good. A bit more compact than what I first came up with.

Thinking about what you are more likely to know and understand I offer 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
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
72
73
74
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

// Prototypes.
void CheckFileOpen(std::ifstream& inFile, std::string inFileName);

int main()
{
	std::size_t count{};
	std::string inFileName{ "Input Files.txt" };
	std::string line;
	std::vector<std::string> fileNames;

	std::ifstream inFile(inFileName);

        // <--- Comment out this code and the code in the second while loop to use the function.
	if (!inFile)
	{
		std::cout << "\n File \"" << inFileName << "\" did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(4));  // <--- Needs header files chrono" and "thread".
		return 1;
	}

	//CheckFileOpen(inFile, inFileName);  // <--- Uses function if you can or want to use it.

	// <--- Loads the vector.
	while (std::getline(inFile, line))
		fileNames.emplace_back(line);

	inFile.close();

	// <--- Processes the vector.
	while (count < fileNames.size())
	{
		inFile.open(fileNames[count]);

		if (!inFile)
		{
			std::cout << "\n File \"" << fileNames[count] << "\" did not open" << std::endl;
			std::this_thread::sleep_for(std::chrono::seconds(4));  // <--- Needs header files chrono" and "thread".
			return 1;
		}

		//CheckFileOpen(inFile, fileNames[count]);  // <--- Uses function if you can or want to use it.

		//  <--- Your code here.


		inFile.close();
		count++;
	}


	// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
	// <--- Used to keep the console window open in Visual Studio Debug mode.
	// 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();

	return 0;
}

void CheckFileOpen(std::ifstream& inFile, std::string inFileName)
{
	if (!inFile)
	{
		std::cout << "\n File \"" << inFileName << "\" did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(4));  // <--- Needs header files chrono" and "thread".
		exit(1);  // <--- Need "exit" here because it is in a function.
	}
}


Use what is best for what you know.

Hope that helps,

Andy
There might not be a need to store the files in a vector if you just need to parse them one after another. Modifying Thomas's code to show what I mean:
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

bool parseFile(istream &is)
{
    // CODE TO PARSE ONE FILE
    return true;
}

bool parseFileOfFiles(istream &is)
{
  string line;
  while(getline(is, line)) {
      ifstream file(line);
      if (!parseFile(file)) return false;
  }
  return is.eof();		// if everything succeeded, you'll hit EOF
}

int main() 
{
    ifstream fileOfFiles("input.txt");
    return !parseFileOfFiles(fileOfFiles);
}

Topic archived. No new replies allowed.