Thread: Piping problem

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    2

    Piping problem

    I get garbage output when printing the string s.
    This is after obtaining the output from xrandr -q, which will list monitor devices and resolutions.
    I use this to determine if VGA1 has diconnected, and if so turn on display LVDS1 and exit the program.

    If you can tell me why s contains garbage i would be grateful.

    Code:
    #include <string.h>
    #include <stdio.h>
    //Searches a string for string in var 'me', returns the starting position of the found string +1 if successfull.
    //On failure it returns 0.
    unsigned int str_find(const char*str, const char* me, unsigned int s){
            
            unsigned int x = 0, l = strlen(me) - 1;
            while( (str[s] ^ '\0') ){
                if ( !(str[s]^me[x]) ){
                    //cout<<"Equal at " << s << ',' << x << endl;
                    if ( !(x^l) )
                        return s - x + 1;
                        
                    if ( (str[s+1]^me[x+1]) )
                        s -= x-2, x = 0;
                    else ++x;
                }
                ++s;
            } 
            return 0;
    }
    
    
    int main(){
      
      FILE *f;
      int c;
      unsigned short n;
      char s[1024];
      while( (f=popen("xrandr -q", "r")) ){
        
        for( n = 0; (c=fgetc(f) != EOF ); ++n )
          s[n] = c;
        s[n] = '\0';
    
    
        pclose(f);
        
        printf("%s",s);
        
        if( str_find("VGA1 disconnected", s, 0) > 0 ){
          printf("VGA1 Disconnected, igniting LVDS1 and exiting program\n");
          system("xrandr --output LVDS1 --auto");
          return 0;
          }
          
      }
      
      return 1;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you could probably start by removing all those silly ^ usage, and replace with more obvious and readable !=

    Then perhaps lookup what the strstr() function does in string.h


    > for( n = 0; (c=fgetc(f) != EOF ); ++n )
    Next, lookup operator precedence, and realise that = is lower than !=

    In which case, you probably want
    for( n = 0; (c=(fgetc(f) != EOF) ); ++n )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    2
    Thank you, and sorry about the ^'s.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Piping problem!
    By Elysia in forum Linux Programming
    Replies: 17
    Last Post: 11-15-2011, 06:36 PM
  2. Implementation of Shell -- Problem in multiple piping
    By ani_pwl in forum C Programming
    Replies: 4
    Last Post: 10-22-2011, 10:00 AM
  3. Problem with piping in my simple UNIX shell
    By Integral Birth in forum C Programming
    Replies: 0
    Last Post: 11-28-2010, 07:37 PM
  4. programming unix shell piping problem
    By Kyro in forum Linux Programming
    Replies: 2
    Last Post: 08-28-2003, 07:52 AM