Results 1 to 3 of 3
I want to write a program, that prints filenames, that might contain spaces, and use this output with other programs, e.g.
Code:
$ ls `myprogram`
The filenames are saved as ...
- 05-09-2008 #1Just Joined!
- Join Date
- May 2008
- Posts
- 7
[C++] How to print filenames with spaces?
I want to write a program, that prints filenames, that might contain spaces, and use this output with other programs, e.g.
The filenames are saved as const char* in my program, this example shows my problem:Code:$ ls `myprogram`
The ouput of this program is interpreted as the 2 files "a" and "b", but it actually should be the file "a b". How can i change that?Code:#include <iostream> int main() { const char* file = "a b"; std::cout << file << std::endl; }
- 05-09-2008 #2Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
It depends on what are you exactly trying to do, but it shouldn't have anything to do with your program, but with the shell you are using. For example, in bash you must adequately quote everything.
Or:Code:ls -l "$(./myprogram)"
Code:for i in $(./myprogram); do ls -l "$i"; done
- 05-09-2008 #3Just Joined!
- Join Date
- May 2008
- Posts
- 7
I use zsh, but it should also work with bash.
this just does the job if there's one file; the program however prints an arbitrary number of files and at least one
your second example got me thinking, it works if the files are separated with "\n":Code:ls -l "$(./myprogram)"
find handles this issue obviously the same way, so i guess it is the best possible wayCode:./myprogram | while read i ; do ls -l "$i"; done


Reply With Quote
