![]() |
|
|
#166 | |
|
Basketry That Evening!
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88
722110 Posts |
Quote:
Code:
printf("Please enter the source text (empty line to quit)\n");
char * string = (char *)malloc( MAXSTRLEN * sizeof(char));
string = fgets(string, MAXSTRLEN, stdin);
for(i=0; string[i]; i++); // i becomes the index of the null, which is the length of the string
if( i == (MAXSTRLEN - 1) ) { // Then the string is longer than MAXSTRLEN - 1, and so the rest is lost.
printf("Source is longer than %d characters. The rest is lost.\n", MAXSTRLEN-1);
}
if ( string[0]==0 ) { // The entire string is null, or rather, the only character was \n, so no input.
printf("Bye.\n");
return 7;
}
1) Why does it go around without waiting for the user the first time through, but it does wait for user input the second time through, and 2) Why doesn't a blank initiate the "Bye" section, or better yet, how does fgets treat the "\n" string? Last fiddled with by Dubslow on 2012-02-23 at 23:28 |
|
|
|
|
|
|
#167 |
|
If I May
"Chris Halsall"
Sep 2002
Barbados
2·67·73 Posts |
|
|
|
|
|
|
#168 | |
|
Basketry That Evening!
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88
3×29×83 Posts |
Quote:
Code:
char * string = (char *)malloc( MAXSTRLEN * sizeof(char));
fgets(string, MAXSTRLEN, stdin);
i = (int)strlen(string);
if( i == (MAXSTRLEN - 1) ) { // Then the string is longer than MAXSTRLEN - 1, and so the rest is lost.
printf("Source is longer than %d characters. The rest is lost.\n", MAXSTRLEN-1); // will add in realloc later once the behavior is correct
}
if ( string[0]==0 ) { // The entire string is null, or rather, the only character was \n, so no input.
printf("Bye.\n");
return 7;
}
Last fiddled with by Dubslow on 2012-02-23 at 23:46 |
|
|
|
|
|
|
#169 |
|
If I May
"Chris Halsall"
Sep 2002
Barbados
230668 Posts |
|
|
|
|
|
|
#170 |
|
Basketry That Evening!
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88
3·29·83 Posts |
|
|
|
|
|
|
#171 | |
|
If I May
"Chris Halsall"
Sep 2002
Barbados
2×67×73 Posts |
Quote:
Empty strings are trivial, so long as you are expecting them... BTW, you might want to look into "buffered" vs. "unbuffered" input and output.... |
|
|
|
|
|
|
#172 |
|
Basketry That Evening!
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88
11100001101012 Posts |
Yeah, I've been wondering about that... I saw the P95 uses sprintf( buf, ...). On the other hand, it seems my if statement to catch empties is not working, but I also have a lab in ten minutes.
|
|
|
|
|
|
#173 |
|
Romulan Interpreter
Jun 2011
Thailand
3·3,221 Posts |
I would read the characters one by one (contrary to line by line) and always keep the last read in a var, initialized to 0 before reading something.
Code:
oldchar=0; newchar=read_new_char(); \\wait for it, with a timer for timeout if the user is sleeping, return zero if it is cr, lf, whatever are you interested in parsing, etc. if (!(olchar|newchar)) exit; oldchar=newchar; //here do whatever you like and loop back at the beginning (for, while, goto, etc). And sideways, it will take care of the empty strings too, because a "empty line" (after some text, or after nothing) is a sequence of two zeros (or two CR, etc). Last fiddled with by LaurV on 2012-02-24 at 02:32 |
|
|
|
|
|
#174 |
|
Aug 2005
Seattle, WA
110111001102 Posts |
Since *you* are apparently familiar with buffered vs unbuffered input, I'm wondering why you think that getchar is so "terribly inefficient from an OS perspective". I believe that from an OS perspective you'll find it to be exactly as efficient as fgets.
|
|
|
|
|
|
#175 |
|
If I May
"Chris Halsall"
Sep 2002
Barbados
100110001101102 Posts |
|
|
|
|
|
|
#176 |
|
Basketry That Evening!
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88
1C3516 Posts |
But is not the latter a front end for the former? As in, my own loop accomplished exactly the same work, and there's not much you can do to make it more efficient.
Last fiddled with by Dubslow on 2012-02-24 at 04:16 |
|
|
|