1

This is a main file that I am using to test methods before I implement them. I am trying to get the list of all files in a directory, write them to a txt file (It works fine until here), then read the file names from that text file.

using namespace std;

string sysCall = "", location = "~/Documents/filenames.txt";
string temp = "";
sysCall = "ls / > "+location;
system(sysCall.c_str());

ifstream allfiles(location.c_str());
allfiles.good();
getline(allfiles, temp);
cout<<temp<<endl;  //At this point, the value of temp is equal to ""

return -1;

After the program runs, no text has been outputted. From what I've read in other peoples' questions, this should work (but obviously doesn't). What am I doing wrong here?

EDIT: allfiles.good() returns false, but I don't understand why it would return that...

mrswmmr
  • 2,081
  • 5
  • 21
  • 23
  • Does the file `~/Documents/filenames.txt` contain the expected list of files after you run the program, i.e. is it only the reading that goes wrong? – HighCommander4 Jun 10 '11 at 19:18
  • 2
    Also, calling `allfiles.good()` by itself like that doesn't do anything. You should **check** whether `allfiles.good()` returns `true` or `false`. If it's `false`, the file wasn't opened correctly and there is a problem with your filename. – HighCommander4 Jun 10 '11 at 19:20
  • yes, the filenames.txt file contains a list of the expected filenames. It is only reading them that is a problem – mrswmmr Jun 10 '11 at 19:21

3 Answers3

7

ifstream allfiles("~/Documents/filenames.txt"); doesn't do what you think it does. The tilde ~ character is not part of the filename -- it is a special character interpreted by some shells. You need the entire path, with no ~ or $ characters in it.

Try setting location to "/tmp/filenames.txt", or just "filenames.txt".

Also, if Boost.Filesystem is available to you, you could use a directory_iterator instead of invoking /bin/ls.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
4

I'll bet the system() call expands the ~ in the filename to your home directory (e.g. /home/mrswmmr), but ifstream does not. Replace the ~ with the full path to your home directory and it should work.

HighCommander4
  • 50,428
  • 24
  • 122
  • 194
0

It has no guarantee to work because system gives no guarantee.

Luc Danton
  • 34,649
  • 6
  • 70
  • 114
  • The system call works, and it writes the text file just fine. It's the ifstream and getline that aren't working – mrswmmr Jun 10 '11 at 19:19
  • Just out of curiosity, what exactly is "gives no guarantee" supposed to mean? If the `system()` call returns 0, does that not guarantee that the command has been executed and exited with a return code of 0? – HighCommander4 Jun 10 '11 at 19:28
  • @HighCommander4 The C++ Standard defers to C for the specification of `system`. Unfortunately I do not have access to a copy of the C standard. However, from the C99 draft (and I have no reason to believe this changed from C89): "information may be returned from the called program in two ways: through the implementation-defined return value [...]". So an implementation that always returns 42 when the argument is non-null is conforming. (The other way mentioned in the snippet is about files.) – Luc Danton Jun 10 '11 at 19:51
  • I do not have a quote as for the argument passed to `system` because the guarantees are somewhat scattered. But a program is able to query if `system` behaves well: if `system(0)` is true then that means `system` will understand commands. So that is the one guarantee. – Luc Danton Jun 10 '11 at 19:55