Skip to main content
GameDev.net gamedev.net
🔒 Locked

basic c question...

Started by FreJa Sep 27, 2005 at 12:49 PM 3 replies 750+ views
Original Post
FreJa
FreJa
Hi, I want to use fgets to read from stdin, so: fgets(str, MAXINT, stdin); however, I dont know the size of str in advance, so how do I define/alloc it ? Huge Thanks
"Through me the road to the city of desolation,Through me the road to sorrows diuturnal,Through me the road among the lost creation."
fnm
fnm
you dont need to know the exact size of data; instead, declare a buffer big enough to contain a line of data, something like this:

#define SHOULD_BE_ENOUGH 1024

char buffer[SHOULD_BE_ENOUGH];
fgets(bufer,SHOULD_BE_ENOUGH,fp);
Promit
Promit
Quote:
Original post by fnm
you dont need to know the exact size of data; instead, declare a buffer big enough to contain a line of data, something like this:

#define I_HOPE_THIS_IS_ENOUGH 1024

char buffer[I_HOPE_THIS_IS_ENOUGH];
fgets(bufer,I_HOPE_THIS_IS_ENOUGH,fp);


Made some edits.

The point being, there's no guarantee you won't be fed a line that's a hundred million characters long.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
kSquared
kSquared
Quote:
Original post by FreJa
Hi,

I want to use fgets to read from stdin, so:

fgets(str, MAXINT, stdin);

however, I dont know the size of str in advance, so how do I define/alloc it ?

Huge Thanks

Take a look at this usenet entry on Google Groups, which pretty much describes your problem.
- k2 "Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ<

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.