Dynamically Getting File Names

I am trying to write a program that dynamically gets the file names of certain files in the current directory that follow a certain format. I am trying the following code.

1
2
3
4
5
6
std::stringstream convert;
convert << system("ls *.csv");
std:: string ls=convert.str();
int start=ls.find_first_not_of(' ');
int stop=ls.find_first_of(' ',start);
std::string firstFile=ls.substr(start,stop-start+1)


This code does not work. If I use cout to display my variables I am getting something I do not expect.

1
2
3
>./a.out
1.csv	2.csv	3.csv	4.csv
0


Why is the 0 there at the end?

Also, if I use a cout to check the values of start and stop I get start=0 and stop=-1.
system("ls *.csv")Why are you calling system? Do you know what it does and what it returns?

And what is the effect of:
 
convert << system("ls *.csv");
system(const char*) only returns an integer, where 0 indicates success, and any other value indicates failure. I see what you are trying to do here, but it won't work quite that easily in c++. You could do something like this using a bash script, but if you need the filenames in a c++ program, you will need to use a platform specific library for accessing the filesystem. For a nice cross platform abstraction for filesystem access I would recommend using the boost filesystem library.
Topic archived. No new replies allowed.