Unix command result to a variable - char?

Start with popen That will let you run a command with its standard output directed to a FILE that your parent can read. From there it's just a matter of reading its output like you would any normal file (e.g. , with fgets getchar etc.).

Start with popen. That will let you run a command with its standard output directed to a FILE * that your parent can read. From there it's just a matter of reading its output like you would any normal file (e.g. , with fgets, getchar, etc. ) Generally, however, you'd prefer to avoid running an external program for that -- you should have getcwd available, which will give the same result much more directly.

It's not part of C's standard library, but it is POSIX, and it's very widely supported. Anyway, if pwd was just an example, have a look at popen(). That will run an external command and give you a FILE* with which to read its output.

Thanks but "pwd" is just an example, I am looking for a generalize solution for any command. – hari Mar 28 at 1:51 You might want to read the second paragraph too. :-) – Sherm Pendley Mar 28 at 1:53 doh... thanks much.

– hari Mar 28 at 2:24.

There is a POSIX function, getcwd() for this - I'd use that.

Thanks but "pwd" is just an example, I am looking for a generalize solution for any command. – hari Mar 28 at 1:50.

Include #include #include int main(int argc, char* argv) { char *dir; dir = getcwd(NULL, 0); printf("Current directory is: %s\n", dir); free(dir); return 0; } I'm lazy, and like the NULL, 0 parameters, which is a GNU extension to allocate as large a buffer as necessary to hold the full pathname. (It can probably still fail, if you're buried a few hundred thousand characters deep. ) Because it is allocated for you, you need to free(3) it when you're done.

I'm done with it quickly, so I free(3) it quickly, but that might not be how you need to use it.

You can fork and use one of the execv* functions to call pwd from your C program, but getting the result of that would be messy at best. The proper way to get the current working directory in a C program is to call char* getcwd(char* name, size_t size).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions