Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dvince2

macrumors 6502
Original poster
Mar 6, 2007
283
1
Canada
Hey everyone
I've tried googling this, but I can't seem to find anything close to what I'm doing.
I have to read in a line that the user enters in the terminal... sort of like this:

Enter a Sentence
> This is a sentence entered by the user.

I'd like to use fgets() to accomplish this, but I'm not sure how to pass the console in as a parameter. Every example I can find is fgets(line, LINE_MAX, fp), where line is my char array, LINE_MAX is the max to read in, and fp is a pointer to a file. How can I point to the console?

Thanks for your help with this n00b question!
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
So fgets with stdin is preferred over gets? Just curious, but what's the reasoning?

Edit: nevermind. Found this which explains it.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX

I obviously agree that fgets is the right answer here, and the link provided has good information, but:
Termination of another, unrelated program

This is not really a risk on most modern OSs. Most processes have their own protected memory space now, so one program going crazy should not affect the memory space of others. If a program attaches to a shared memory segment it could easily corrupt that, and that could cause issues for other processes, but in the general case you shouldn't be able to do worse than killing the particular program in which the problem exists. This isn't to downplay the risk, but I think understanding things like this can be important for beginners.

-Lee
 

dvince2

macrumors 6502
Original poster
Mar 6, 2007
283
1
Canada
Thanks a lot everyone! It never occurred to me that i could use stdin like that lol.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.