Advertisement
Mysoft

Untitled

Jun 3rd, 2016
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2.  
  3. char name[27];
  4.  
  5. int ReadLine( char* buffer , int maxLen )
  6. {
  7.     int len;
  8.  
  9.     //read up to maxlen+1 chars (including the '\n')
  10.     fgets(buffer,maxLen+1,stdin);
  11.     len = strlen(buffer);
  12.  
  13.     //there is a '\n' at the end?
  14.     if(buffer[len-1] == '\n')
  15.     {    //yes... so <= maxlen
  16.         (buffer[len-1] = '\0'; //turn \n into ending...
  17.     }
  18.     else //no... so > maxlen (will return maxlen+1 as size)
  19.     {
  20.         // read the rest of the line...
  21.         while ( !strchr( buffer , '\n') ) { fgets(buffer,maxLen+1,stdin); }
  22.     }
  23.  
  24.     return len;
  25.  
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement