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!
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.
#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.
}
}
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: