Hey guys, I'm working on something and need a hand. I've been lurking the site for a while now, but this is my first post. I'm trying to write a c++ program that will read in text files and put the values into a vector, but I want it to do it without knowing the filenames.
Basically, I have one program that you run, and it asks the user a bunch of questions and outputs the answers to a file. The file is <answer to question #1 (what is your name)>.txt
I want to put say, six of those name.txt files {mike.txt, evan.txt, ryan.txt, max.txt, mark.txt, ben.txt} into a folder, and run a second program and have it read in all of the answers from all six programs into a vector. Then next time, say it's {john.txt, ivan.txt, billy.txt, joe.txt, sally.txt, jenny.txt, sandy.txt, sue.txt, teresa.txt, philly.txt} (ten this time, instead of six). I have everything set up other than the unknown file name part.
Does anyone know how to do this?
Here's my code:
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
|
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <vector>
using namespace std;
int main ()
{
string curMember;
vector<string> memberList;
int count;
ifstream memberFile;
ofstream fullList;
for (int x=0; /*x < number of files in folder to be opened*/; x++)
{
memberFile.open (/*unkown named text file*/);
if (memberFile.is_open)
{
getline (memberFile, curMember);
memberList.push_back(curMember);
}
else
{
cout << "Error opening file" << endl;
return 1;
}
}
fullList << memberList;
system("PAUSE");
return 0;
}
|