简体   繁体   中英

Linux C++ file permission denied

ls -alR | myprogram

If there're files that I don't have access to, it'll give an error message saying like "file permission denied". But how to detect this error in my program? I was trying to test if there're data in stderr . But that didn't seem to work.

For your immediate problem, you are only piping stdout here. To capture both stdout and stderr, do this:

ls -alR 2>&1 | myprogram

See http://www.gnu.org/software/bash/manual/bashref.html#Pipelines from the Bash manual.

However, it's not clear why you'd want to do this. If you want to programmatically do stuff with directory listings, there are better ways to do it than parsing the output of ls . If you edit your question to describe your overall goal, I may be able to give more specific advice...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM