Trying to read unknown file names into an array

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;
}
Last edited on
A simple thing to do is to make your other program also create a names.txt file where it puts the filenames you need and have your current program read that file. Otherwise, you'll probably have to use OS specific stuff.

Useful link -> http://stackoverflow.com/questions/612097/how-can-i-get-a-list-of-files-in-a-directory-using-c-or-c
Last edited on
That's a REALLY awesome idea, but I can't figure out how to implement it. The first program I made is a simple one that I'm sending to multiple different people. They are going to run it, and send me back the output file.

If I was going to be the one running the program repeatedly myself, that would work, but how would I do that if they're the ones that are going to be running the program individually and sending me the output?
OS specific stuff it is, then. What operating system are you using? Did you check the link I posted above?
I'm using windows, I'm on win7x64, but the people using the program will probably be using anything from XP to 8 both x86 and x64 =/

I'm reading through it now. That looks a whole heck of a lot like greek to me, honestly. I'm in an into to programming class and doing research on the side. I understand the concept of header files, and roughly how to use them, but I wouldn't even know how to call this inside a program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
DIR *dir;
struct dirent *ent;
    dir = opendir ("c:\\src\\");
if (dir != NULL) {

  /* print all the files and directories within directory */
  while ((ent = readdir (dir)) != NULL) {
    printf ("%s\n", ent->d_name);
  }
  closedir (dir);
} else {
  /* could not open directory */
  perror ("");
  return EXIT_FAILURE;
}
the people using the program will probably be using anything from XP to 8 both x86 and x64 =/

Wait, are they going to run your second program too? If not, this is not important.

Since you're on windows, you could use FindFirstFile / FindNextFile to get your list of files:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <windows.h>
#include <cstdio>

int main()
{
   WIN32_FIND_DATA findFileData;
   HANDLE hFind;

   printf("list of text files in C:\\ ...\n\n");

   hFind = FindFirstFile("C:\\*.txt", &findFileData);

   if (hFind != INVALID_HANDLE_VALUE)
   {
       do printf("%s\n", findFileData.cFileName);
       while (FindNextFile(hFind, &findFileData) != 0);

       FindClose(hFind);
   }
}

Another useful link -> http://msdn.microsoft.com/en-us/library/windows/desktop/aa365200%28v=vs.85%29.aspx

EDIT:

I wouldn't even know how to call this inside a program

Well, there really isn't a simpler way to do it. You'll have to read the documentation on the functions and data structures you're going to use, understand what the parameters and return values of each function represent and move on from there. One thing is certain: If you're not willing to do some work, you'll never get a satisfactory result.
Last edited on
Well, there really isn't a simpler way to do it. You'll have to read the documentation on the functions and data structures you're going to use, understand what the parameters and return values of each function represent and move on from there. One thing is certain: If you're not willing to do some work, you'll never get a satisfactory result.


I understand. I'm more than willing to put in the time and research to learn how to do this. As of two days ago when I started this whole project, I didn't know what vectors were, or how to read from or write to a file, or a large amount of other things. My point being, I'm not looking to have someone do the work for me, I'm just not understanding the material.

Wait, are they going to run your second program too? If not, this is not important.

Yeah, nvm. I said that without thinking.
I'm more than willing to put in the time and research to learn how to do this. As of two days ago when I started this whole project, I didn't know what vectors were, or how to read from or write to a file, or a large amount of other things.

That's good to hear :)

On second thought, there might be an easier way...

You could use a system command to do the listing for you, output the
result to a file, and then parse that file and get the data you want:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cstdlib>
#include <fstream>

//...

int main()
{
    system("dir C:\\*.txt /B > C:\\filenames.txt");

    // open filenames and get the data you want

    std::ifstream fin("C:\\filenames.txt");
    
    //...
}

Note, though, that using system is not a good option in general,
for several reasons: http://www.cplusplus.com/articles/j3wTURfi/

EDIT: I added the /B option in the dir command above.
Now, you only get the file names (no dates and stuff).

Yet another useful link -> http://www.easydos.com/dir.html
Last edited on
Note that normal users don't have access to C:\ so Rm4ster r0shi's system example, as-is, won't run on Windows versions after Windows XP.

Alter the paths to point at a folder where you have write permission.

Andy
Weird... The above works fine on my Windows 7 Ultimate laptop.
I think it's down to what user you're running as.

Whoever you are, you have access to files in %HOMEDRIVE%%HOMEPATH%, and that's defined on the OSs you talked about.

To find out what folder %HOMEDRIVE%%HOMEPATH% refers to, open Run (Windows Logo Key + R on keyboard) and then type:
explorer "%HOMEDRIVE%%HOMEPATH%"

and it will open the folder it refers to.

Every user should have access to this folder, as it refers to your own user folder.

EDIT: e.g. on my Windows 7 system it refers to C:\Users\Veltas
and on Windows XP it would've referred to C:\Documents and Settings\Veltas
Last edited on
@Rm4ster r0sh

Well, I get an error when I try to open a file for write on my Windows 7 laptop.

If it works for you then either (a) the account you log on with has administratuve privileges, which is a bad idea, or (b) you have altered the access permissions for C:\, which is worse.

Edit: Just had a quick Google. There is one get out: if the Windows 7 installation is an upgrade from an earlier version of Windows, then C:\ might have write access. But for clean installs, my above comments hold true.

Anyway...

Note that the write access applies to files. You can, as a regular user, create new subfolders in C:\

And rather than use environments variables, a Win32 program should use SHGetFolderPath (with CSIDL_PROFILE) to obtain the user's folder path.

SHGetFolderPath function (Windows)
http://msdn.microsoft.com/en-gb/library/windows/desktop/bb762181%28v=vs.85%29.aspx

Even better, you can use CSIDL_APPDATA to obtain the Application Data folder directly and create a subfolder there for your app to use.

Note that the function SHGetKnownFolderPath does the same kind of thing, but is only available on Windows Vista and newer.

Andy
Last edited on
the account you log on with has administratuve privileges, which is a bad idea

Why is this a bad idea if I'm the only one using the laptop? If I don't
have administrative privileges on my laptop, who is supposed to have?

Anyway, IMO, this is a broken, useless feature of the more recent versions of windows.

http://en.wikipedia.org/wiki/User_Account_Control says:
It aims to improve the security of Microsoft Windows by limiting application software to standard user privileges until an administrator authorizes an increase or elevation. In this way, only applications trusted by the user may receive administrative privileges, and malware should be kept from compromising the operating system. In other words, a user account may have administrator privileges assigned to it, but applications that the user runs do not inherit those privileges unless they are approved beforehand or the user explicitly authorizes it.

It's broken because, when you ran my snippet above, it
didn't ask for your permission. It made the decision itself.

And it's useless because you can very easily bypass it ( even
programmatically : http://stackoverflow.com/a/6418873 ).

EDIT: Unless we're not talking about the same thing...
Last edited on
Fixed that too, never mind. Thanks for the help all!
Last edited on
Okay. So. I've hit a wall. I don't know if what I'm trying to do is possible.

The idea here is that I'm making a program for a murder mystery type game. You have a group of people with a set of classifications (age, experiences, height, occupation). You're given clues about who the murderer is, and you need to figure out which of your group has done it.

I have one program that asks each person about their info and outputs it to a file. I've made another program that reads in each of those outputs and compile them into one .txt file. Each input is on it's own line. Now I'm trying to figure out a way to do the following:

I need to read in all of the outputs, then tell the program what clues we have (they come in no specific order) and I'm looking for it to spit out a list of possibles until we get enough clues to figure out who done it!

Is something like this even possible?
Topic archived. No new replies allowed.