Save system() into char[]


 
Thread Tools Search this Thread
Top Forums Programming Save system() into char[]
# 1  
Old 04-12-2012
Save system() into char[]

Hi!
I have something like this :
Code:
...
char buffer[25] = "ls -l";
system(buffer);
...

and i just want to save what i get to the terminal from ls -l into a char.
I first think of doing something like ls -l>text.txt and after opening the file in my program and fscanf(). Any faster way ?
Smilie

Last edited by giampoul; 04-12-2012 at 10:13 AM..
# 2  
Old 04-12-2012
Code:
char buf[512];
FILE *fp=popen("ls -l", "r");

while(fgets(fp, 512, fp))
{
...
}

pclose(fp);

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 04-12-2012
Quote:
Originally Posted by Corona688
Code:
char buf[512];
FILE *fp=popen("ls -l", "r");

while(fgets(fp, 512, fp))
{
...
}

pclose(fp);

Thanks! I used this code :
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
	char str[20] = "ls -l text.txt";
	FILE *fp;
	int status;
	char terminal[512];
	
	fp = popen(str, "r");
	if( fp == NULL )
	{
		printf("popen error\n");
		exit(EXIT_FAILURE);
	}
	fgets(terminal, 512, fp);
	status = pclose(fp);
	if( status == -1 )
	{
		printf("pclose error\n");
		exit(EXIT_FAILURE);
	}
	printf("Terminal will give this:\n%s", terminal);
}

and it gives me what i wanted...
I didnt understand in your example the while loop use...
But this works! Thanks!
# 4  
Old 04-12-2012
Quote:
Originally Posted by giampoul
I didnt understand in your example the while loop use...
Because it's completely possible for a program to print more than one line. Smilie fgets() just reads the lines one at a time. Inside the loop, you can use the contents of the line as you see fit.

The loop breaks when it hits EOF because fgets() will return null if it's unable to read a line, which if/while/etc will consider to be a false value.

In the end I suppose it doesn't matter, but I've made it a habit to read until EOF so the program you're reading from can quit cleanly instead of getting killed by sigpipe.

Last edited by Corona688; 04-12-2012 at 01:58 PM..
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 04-16-2012
don't forget to use W macros (WEXITSTATUS , etc...) when checking return status of pclose
This User Gave Thanks to migurus For This Post:
# 6  
Old 04-18-2012
Quote:
Originally Posted by migurus
don't forget to use W macros (WEXITSTATUS , etc...) when checking return status of pclose
A little more about this? Maybe an example? Smilie
# 7  
Old 04-18-2012
These macros are used to check process status. The pclose() call does report exit status of the process which has been run by corresponding popen().

Code:
int rc;
rc = pclose(pfp);
printf("rc=%i\n", WEXITSTATUS(rc));

Just bear in mind when examine a return code of a command, make sure IF there is a wrapper shell that executes the program which code you expect to see, then the wrapper needs to store your program's exit code and return it.
This User Gave Thanks to migurus For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Save value from output of Corestat and save in a list for each core

I am trying to modify the "corestat v1.1" code which is in Perl.The typical output of this code is below: Core Utilization CoreId %Usr %Sys %Total ------ ----- ----- ------ 5 4.91 0.01 4.92 6 0.06 ... (0 Replies)
Discussion started by: Zam_1234
0 Replies

2. Programming

Invalid conversion from char* to char

Pointers are seeming to get the best of me and I get that error in my program. Here is the code #include <stdio.h> #include <stdlib.h> #include <string.h> #define REPORTHEADING1 " Employee Pay Hours Gross Tax Net\n" #define REPORTHEADING2 " Name ... (1 Reply)
Discussion started by: Plum
1 Replies

3. UNIX for Advanced & Expert Users

Scan all mail messages through the server and save a copy into file system following a rule

In our company we work for our customer with a job# philosophy, managing all the informations about a job in a share with directories whose name is starting with job number. Under this entry point we have a standard structure of folders, comprising a "communications" folder. When we send emails... (0 Replies)
Discussion started by: vroby67
0 Replies

4. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

5. UNIX for Advanced & Expert Users

Check EOF char in Unix. OR To check file has been received completely from a remote system

Advance Thanks. (1) I would like to know any unix/Linux command to check EOF char in a file. (2) Or Any way I can check a file has been reached completely at machine B from machine A. Note that machine A ftp/scp the file to machine B at unknown time. (5 Replies)
Discussion started by: alexalex1
5 Replies

6. Shell Programming and Scripting

Save cURL verbose output to file or do it like browser "save as.."

hi there ! i have exactly the same problem like this guy here https://www.unix.com/shell-programming-scripting/127668-getting-curl-output-verbose-file.html i am not able to save the curl verbose output.. the sollution in this thread (redirecting stderr to a file) does not work for me.... (0 Replies)
Discussion started by: crabmeat
0 Replies

7. Programming

pass char string via system()

hello all i am trying to pass a argument through system function to a shell script. #shell script echo.sh to display the string echo $1 and the c program is. #include<stdlib.h> int main() { const char *str = "hello all"; system("sh echo.sh str"); } the output i... (5 Replies)
Discussion started by: zius_oram
5 Replies

8. Programming

concat const char * with char *

hello everybody! i have aproblem! i dont know how to concatenate const char* with char const char *buffer; char *b; sprintf(b,"result.txt"); strcat(buffer,b); thanx in advance (4 Replies)
Discussion started by: nicos
4 Replies

9. Programming

Adding a single char to a char pointer.

Hello, I'm trying to write a method which will return the extension of a file given the file's name, e.g. test.txt should return txt. I'm using C so am limited to char pointers and arrays. Here is the code as I have it: char* getext(char *file) { char *extension; int i, j;... (5 Replies)
Discussion started by: pallak7
5 Replies

10. Shell Programming and Scripting

How to replace any char with newline char.

Hi, How to replace any character in a file with a newline character using sed .. Ex: To replace ',' with newline Input: abcd,efgh,ijkl,mnop Output: abcd efgh ijkl mnop Thnx in advance. Regards, Sasidhar (5 Replies)
Discussion started by: mightysam
5 Replies
Login or Register to Ask a Question