Hmmm...Accessing the environment from a C++ program. Maybe?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hmmm...Accessing the environment from a C++ program. Maybe?
I want to write a short c++ program that asks a user for a path. I then want the program to scan the files in the path and write a list of those files into another file. I have only begun to do some programming in c++.
I essentially want to do (something similar to) ls /to/some/path >> file.txt. It would be really nice to get some pointers on how to filter out and have say files ending in .ogg be the only ones put into my file.txt.
I want this to run under linux (of course), but if there is a more generic way of doing this that would allow me to cross compile for windows that would be interesting to know too.
`opendir' the location, then `readdir' the files, `strstr' to determine if they are of type ogg, at the end, `closedir'. look into the man pages of the functions.
Great! Thanks for the quit replies. I never knew that I could so easily ... call to the system (if that is the way it's put ) using "system." As far as using opendir and readdir those seem like more mind expanding options so I think I will try those too. Just for fun.
One more question. I just looked at man opendir and (I haven't gotten to this in c++ class yet so yes it is premature but>>>) how would I do error handling for opendir?
Would I use some kind of if statement?
(From man opendir)
ERRORS
EACCES Permission denied.
EMFILE Too many file descriptors in use by process.
ENFILE Too many files are currently open in the system.
ENOENT Directory does not exist, or name is an empty string.
ENOMEM Insufficient memory to complete the operation.
ENOTDIR
name is not a directory.
If ( something (im not sure what!) == ENFILE) return ENFILE;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.