SHOW:
|
|
- or go back to the newest paste.
1 | #include <stdio.h> | |
2 | ||
3 | char name[27]; | |
4 | - | int namlen; |
4 | + | |
5 | int ReadLine( char* buffer , int maxLen ) | |
6 | - | printf("Enter a series of names and type 'end' to stop:\n"); |
6 | + | |
7 | int len; | |
8 | - | do |
8 | + | |
9 | //read up to maxlen+1 chars (including the '\n') | |
10 | - | namelen = strlen(name); |
10 | + | fgets(buffer,maxLen+1,stdin); |
11 | - | if(name[namelen-1] == '\n') |
11 | + | len = strlen(buffer); |
12 | - | name[namelen-1] = '\0'); |
12 | + | |
13 | - | //How and where should this be added? --> while !strchr( name , '\n') { fgets(name,26,stdin); } |
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 | } |