Thread: Rewriting getline with streams

  1. #1
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193

    Question Rewriting getline with streams

    Good afternoon. How can I write the function nonOpGetLine:

    Code:
     
    
    #include <stdio.h> 
    
    
    #ifndef MAXLINE
      #define MAXLINE 100
    #endif  
    
    int nonOpGetLine(char* s, int lim); 
    
    int nonOpGetLine(char* s, int lim)
    { 
      int c;
      int i = 0; 
      while(i < lim - 1) 
      { 
         c = getchar();
         if(c == EOF) 
          break;
         if(c == '\n') 
          break; 
      
        s[i++] = c; 
      }
      s[i] = '\0';
      return i;       
    }     
    
    
    int main()
    { 
      int n;
      char s[MAXLINE];
      while( (n = nonOpGetLine(s, MAXLINE)) > 0)
       fputs(s, stdout);
      putchar('\n');      
      return 0;    
    }
    with streams like fgetc(stdin)? I tried to replace every getchar() with it but it didn't work how I was expecting.

    Code:
     
    #include <stdio.h> 
    
    
    #ifndef MAXLINE
      #define MAXLINE 100
    #endif  
    
    int nonOpGetLine(char* s, int lim); 
    
    int nonOpGetLine(char* s, int lim)
    { 
      int i = 0; 
      while(i < lim - 1) 
      { 
         fgetc(stdin);
         if(fgetc(stdin) == EOF)
          break;
         if(fgetc(stdin) == '\n') 
          break;
         
         s[i++] = fgetc(stdin);    
      }
      s[i] = '\0';
      return i;       
    }     
    
    
    int main()
    { 
      int n;
      char s[MAXLINE];
      while( (n = nonOpGetLine(s, MAXLINE)) > 0)
       fputs(s, stdout);
      putchar('\n');      
      return 0;    
    }
    input:
    thames
    output:
    m

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    From man fgetc:

    fgetc() reads the next character from stream and returns it as an unsigned char cast to an int, or EOF on end of file or error.

    getc() is equivalent to fgetc() except that it may be implemented as a macro which evaluates stream more than once.

    getchar() is equivalent to getc(stdin).
    You replaced your original getchar() by a bunch of fgetc() calls. One call would be reasonable ... and keep the int c variable too.

  3. #3
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    I read earlier the man fgetc but I think I wasn't able to picture the logic. Many thanks, now I fixed it:


    Code:
     
    #include <stdio.h> 
    
    
    #ifndef MAXLINE
      #define MAXLINE 100
    #endif  
    
    int nonOpGetLine(char* s, int lim); 
    
    int nonOpGetLine(char* s, int lim)
    { 
      int i,c; 
      i = 0;
      
      while(i < lim - 1) 
      { 
         c = fgetc(stdin);
         if(c == EOF) 
          break;
         if(c == '\n') 
          break;
           
        s[i++] = c;    
      }
      s[i] = '\0';
      return i;       
    }     
    
    
    int main()
    { 
      int n;
      char s[MAXLINE];
      while( (n = nonOpGetLine(s, MAXLINE)) > 0)
       fputs(s, stdout);
      putchar('\n');      
      return 0;    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help on rewriting
    By lucky4ever in forum C Programming
    Replies: 13
    Last Post: 01-15-2010, 09:50 AM
  2. Help - rewriting style of string library in C
    By ~C_Student~ in forum C Programming
    Replies: 24
    Last Post: 11-19-2009, 09:34 PM
  3. Replies: 0
    Last Post: 07-23-2009, 03:20 AM
  4. rewriting my server
    By xixpsychoxix in forum Networking/Device Communication
    Replies: 5
    Last Post: 07-01-2008, 07:50 PM
  5. Rewriting an opened file.
    By Daghdha in forum C Programming
    Replies: 5
    Last Post: 06-13-2002, 01:55 AM

Tags for this Thread