LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Making system calls in C++ (https://www.linuxquestions.org/questions/programming-9/making-system-calls-in-c-386508/)

harisund 11-25-2005 03:54 PM

Making system calls in C++
 
First, maybe system calls is a wrong word to describe what I am talking about.

While inside a C++ program (or for that matter, even C will be fine) I want to call `ls > FileListing` and then read the FileListing file through the program .

Can it be done?

Basically, I have a lot of directories, and I want to parse all the files in those directories, and then do some indexing of the files.

Does anyone think using a csh would help? If so, in what way? I was wondering if the name is "c" shell, it must have some benefits to C programmers, right?

Thanks a lot

Regards

Hari

bulliver 11-25-2005 08:06 PM

Check out "man 3 system" or "man 3 exec" for how to call a shell command from C/C++

However, you can do this directly from C/C++ using the "glob" library call, given "*" as an argument to return all files in a directory. From "man 3 glob":
Code:

      One example of use is the following code, which simulates typing ls -l *.c ../*.c in the shell.

          glob_t globbuf;

          globbuf.gl_offs = 2;
          glob("*.c", GLOB_DOOFFS, NULL, &globbuf);
          glob("../*.c", GLOB_DOOFFS | GLOB_APPEND, NULL, &globbuf);
          globbuf.gl_pathv[0] = "ls";
          globbuf.gl_pathv[1] = "-l";
          execvp("ls", &globbuf.gl_pathv[0]);

Quote:

Does anyone think using a csh would help? If so, in what way? I was wondering if the name is "c" shell, it must have some benefits to C programmers, right?
csh has nothing to do with C, it is just named that because the syntax is similar to C...

zahadumy 11-25-2005 08:09 PM

man 3 exec

Was it helpful?

*** UPDATE ***
Sorry, bulliver was faster than me.

harisund 11-26-2005 05:15 PM

system was what I was looking for. Thanks a lot.

And by the way, man 3 exec outputs no manual entry for section 3 of exec.

But system works fine.

Thanks a lot !

Regards
hari

zahadumy 11-26-2005 05:58 PM

If you type "man 3 exec" on Fedora Core 3 you will see this. I think you get the same output on Slackware 10.1, but I'm not 100% about this. But if system solved your problem, that's fine. :)


All times are GMT -5. The time now is 08:29 AM.