mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2012-02-23, 23:21   #166
Dubslow
Basketry That Evening!
 
Dubslow's Avatar
 
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88

722110 Posts
Default

Quote:
Originally Posted by chalsall View Post
No. As I said, it's simply a macro -- the execution speed will be exactly the same.

As WraithX points out above, you want to get (no joke intended) friendly with fgets().

You work with what fgets() returns (if anything), and map it into your own data structures (possibily with dynamically allocated storage space) to parce as a function of what it has already been returned in the past.
Okay then:
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;
}
Edit: Okay, at least it sort of works now. This stuff is all part of a while(1) loop. When I run the prog, it goes through this part of the code, but does not wait for user input and just finishes the loop and goes around again, but the second time, it does wait for user input. When it does wait and I do give it "\n" as input, i.e. no input, it just goes through the loop again without exiting properly.
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
Dubslow is offline   Reply With Quote
Old 2012-02-23, 23:26   #167
chalsall
If I May
 
chalsall's Avatar
 
"Chris Halsall"
Sep 2002
Barbados

2·67·73 Posts
Default

Quote:
Originally Posted by Dubslow View Post
Okay then:
Getting there...

Now, wrap your head around the "realloc()" function, and properly deal with a text line which is longer than MAXSTRLEN.

And, as an aside, your "for loop" could be replaced with the strlen() function.
chalsall is offline   Reply With Quote
Old 2012-02-23, 23:45   #168
Dubslow
Basketry That Evening!
 
Dubslow's Avatar
 
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88

3×29×83 Posts
Default

Quote:
Originally Posted by chalsall View Post

And, as an aside, your "for loop" could be replaced with the strlen() function.
strlen, and also removed the redundant string = fgets
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;
		}
However, the behavior is still as above, which is to say, not correct.

Last fiddled with by Dubslow on 2012-02-23 at 23:46
Dubslow is offline   Reply With Quote
Old 2012-02-23, 23:51   #169
chalsall
If I May
 
chalsall's Avatar
 
"Chris Halsall"
Sep 2002
Barbados

230668 Posts
Default

Quote:
Originally Posted by Dubslow View Post
However, the behavior is still as above, which is to say, not correct.
So... Follow my suggestion...

How would you use realloc() to solve the problem?
chalsall is offline   Reply With Quote
Old 2012-02-24, 00:24   #170
Dubslow
Basketry That Evening!
 
Dubslow's Avatar
 
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88

3·29·83 Posts
Default

Quote:
Originally Posted by chalsall View Post
So... Follow my suggestion...

How would you use realloc() to solve the problem?
What does realloc() and strlen()>MAXSTRLEN have to do with incorrect behavior for empty strings?
Dubslow is offline   Reply With Quote
Old 2012-02-24, 00:42   #171
chalsall
If I May
 
chalsall's Avatar
 
"Chris Halsall"
Sep 2002
Barbados

2×67×73 Posts
Default

Quote:
Originally Posted by Dubslow View Post
What does realloc() and strlen()>MAXSTRLEN have to do with incorrect behavior for empty strings?
Sorry... Different problem...

Empty strings are trivial, so long as you are expecting them...

BTW, you might want to look into "buffered" vs. "unbuffered" input and output....
chalsall is offline   Reply With Quote
Old 2012-02-24, 00:48   #172
Dubslow
Basketry That Evening!
 
Dubslow's Avatar
 
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88

11100001101012 Posts
Default

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.
Dubslow is offline   Reply With Quote
Old 2012-02-24, 02:31   #173
LaurV
Romulan Interpreter
 
LaurV's Avatar
 
Jun 2011
Thailand

3·3,221 Posts
Default

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).
Oc course, you can directly initialize and compare with CR or LF (code 10 or 13, hex 0x0A or 0x0D), but you will lose flexibility. This version has the advantage that you move outside parsing of the characters (remember the problem with yafu, which is sending out \b characters, and yoyo's perl script doesn't know what to do with them?). Maybe later you would be interested to parse something else, like \b, \g (beeps?), ctrl+z (eof), instead of empty line, 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
LaurV is offline   Reply With Quote
Old 2012-02-24, 03:56   #174
jyb
 
jyb's Avatar
 
Aug 2005
Seattle, WA

110111001102 Posts
Default

Quote:
Originally Posted by chalsall View Post
Sorry... Different problem...

Empty strings are trivial, so long as you are expecting them...

BTW, you might want to look into "buffered" vs. "unbuffered" input and output....
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.
jyb is offline   Reply With Quote
Old 2012-02-24, 04:04   #175
chalsall
If I May
 
chalsall's Avatar
 
"Chris Halsall"
Sep 2002
Barbados

100110001101102 Posts
Default

Quote:
Originally Posted by jyb View Post
I believe that from an OS perspective you'll find it to be exactly as efficient as fgets.
OK, OS was the wrong term. The point I was trying to make is why call a function (say 70) times to read a line when you can call another once to get the same result?
chalsall is offline   Reply With Quote
Old 2012-02-24, 04:16   #176
Dubslow
Basketry That Evening!
 
Dubslow's Avatar
 
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88

1C3516 Posts
Default

Quote:
Originally Posted by chalsall View Post
OK, OS was the wrong term. The point I was trying to make is why call a function (say 70) times to read a line when you can call another once to get the same result?
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
Dubslow is offline   Reply With Quote
Reply

Thread Tools


All times are UTC. The time now is 08:01.


Fri Aug 6 08:01:02 UTC 2021 up 14 days, 2:30, 1 user, load averages: 2.27, 2.34, 2.42

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.