popen help

This is a discussion on popen help within the C Programming forums, part of the General Programming Boards category; Hello, I'm trying to open a Linux shell script which searches a document for a particular number. I would like ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    14

    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
    }

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    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);
    }
    DavT
    -----------------------------------------------

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    14
    Thanks dav,
    That did it,

    OJ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. popen and fgets, underflow!
    By henrikstolpe in forum Linux Programming
    Replies: 0
    Last Post: 02-06-2009, 02:39 AM
  2. popen takes too long?
    By Largo in forum C Programming
    Replies: 2
    Last Post: 11-20-2006, 05:46 AM
  3. popen()
    By Cactus_Hugger in forum Windows Programming
    Replies: 2
    Last Post: 10-22-2005, 03:16 PM
  4. popen vs fopen
    By esme in forum Linux Programming
    Replies: 1
    Last Post: 11-25-2002, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21