LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-29-2004, 01:03 AM   #1
Kroenecker
Member
 
Registered: May 2003
Location: The States
Distribution: Gentoo
Posts: 245

Rep: Reputation: 30
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.

Any suggestions?
 
Old 09-29-2004, 01:46 AM   #2
Sadrul
LQ Newbie
 
Registered: Mar 2004
Distribution: Slackware 10.0 (K: 2.4.26)
Posts: 13

Rep: Reputation: 0
`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.

-- Adil
 
Old 09-29-2004, 01:48 AM   #3
greg108
Member
 
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170

Rep: Reputation: 30
#include <iostream>

using namespace std;

int main()
{
system("ls /some/path > myfile.txt");
}
 
Old 09-29-2004, 02:00 AM   #4
Kroenecker
Member
 
Registered: May 2003
Location: The States
Distribution: Gentoo
Posts: 245

Original Poster
Rep: Reputation: 30
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.
 
Old 09-29-2004, 02:04 AM   #5
Kroenecker
Member
 
Registered: May 2003
Location: The States
Distribution: Gentoo
Posts: 245

Original Poster
Rep: Reputation: 30
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;

Or something similar?
 
Old 09-29-2004, 02:12 AM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Just above those error codes you listed is this in 'man opendir':
Code:
RETURN VALUE
       The opendir() function returns a pointer to the  directory
       stream or NULL if an error occurred.
So you'd do something like this:
Code:
dir = opendir("/some/path");
if(dir == NULL)
  cout << "An error occured!" << endl;
else
  cout << "We opened it successfully!" << endl;
 
Old 09-29-2004, 02:15 AM   #7
Kroenecker
Member
 
Registered: May 2003
Location: The States
Distribution: Gentoo
Posts: 245

Original Poster
Rep: Reputation: 30
Right-o. Thanks
 
Old 09-29-2004, 02:19 AM   #8
Sadrul
LQ Newbie
 
Registered: Mar 2004
Distribution: Slackware 10.0 (K: 2.4.26)
Posts: 13

Rep: Reputation: 0
expanding from itsme86's code:
Code:
if(dir == NULL)
{
    cout << "An error occured!" << endl;
    cout << strerror(errno) << endl;
}
this will show just what the error is. (you will have to include `errno.h' file.

-- Adil
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to execute a .exe file in linux environment from a c program? aakanksh_c Linux - General 1 09-18-2005 04:26 AM
accessing programs in a multiuser environment hari78 Linux - Newbie 6 04-15-2005 10:25 AM
How to know which program is accessing the Internet. mattmiller Linux - Networking 7 08-05-2004 02:23 PM
accessing /proc from within chroot environment JustinHoMi Linux - Security 7 06-06-2003 03:48 PM
How to tell the shell program is invoked from GUI environment? oldbee Programming 9 01-20-2002 09:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration