-
popen help
Hello,
I'm trying to open a Linux shell script which searches a document for a particular number.
I would like to get this number into a variable for use in my C-program so I can test it.
I use popen and the correct number is displayed by my C program, but I cannot seem to do anything with it. For instance I am unable to get the correct value into another function.
What's going on here? Thanks for any help
OJ
Code:
int main ( )
{
while (1){
char buff[BUFSIZ];
FILE *fp = popen( ". getfile", "r" );
while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
}
pclose( fp );
printf("variable buff is: %s\n",buff);
//This displays "10" which is correct
use_info (*buff);
}
return 0;
}
int use_info (int var) {
printf("Values passed to function is %d\n", var);
//This displays "49" which is incorrect
}
-
Its quite hard from your code to work out what you are trying to acheive, but I think your problem is in the value that you pass to use_info(). You are passing the first character in the string 'buff' as a parameter. The first character you tell us is '1' so this gets converted to an integer (in ascii, '1' == 49) and the value gets printed to the screen. If you want to print out the value stored in the string then you will have to use a conversion function such as atoi(). eg:
Code:
...
printf ("variable buff is: %s\n", buff);
use_info (atoi(buff));
...
int use_info (int var)
{
printf ("value passed to function is %d\n", var);
}
-
Thanks dav,
That did it,
OJ