-  [WT]  [Home] [Manage]

[Return]
Posting mode: Reply
Name
Email
Subject   (reply to 7)
Message
Captcha
File
Embed   Help
Password  (for post and file deletion)
  • Supported file types are: 7Z, GIF, JPG, PDF, PNG, RAR, TXT, XZ, ZIP
  • Maximum file size allowed is 10240 KB.
  • Images greater than 200x200 pixels will be thumbnailed.
  • Currently 112 unique user posts. View catalog

  • Blotter updated: 2012-05-14 Show/Hide Show All

File 12897982421.jpg - (52.76KB , 800x504 , Invader_Zim_Gir_and_Squirrel_by_Deansta.jpg ) Thumbnail displayed, click image for full size.
7 No. 7
When you put aside a language for a while, it's surprising to deal with all the little gotchas that come with it.

Today I was noodling around with C, just working on a toy project. At one point I wanted to retrieve a character as input from the console (the user types in a number). I decided to do this by using getc(), but discovered that pressing the Return key left a newline character in stdin. That meant that when I called getc() again, the newline was automatically retrieved before I could type in a new character.

I decided to switch gears and use fgets() to put the input into a character array. fgets() turns the ending newline character into a null character for the array, so I thought this would work out. Surprisingly, the same thing kept happening. A newline character was getting stuck in stdin and would be automatically retrieved the next time I needed a character.

In the end I went back to getc(), did whatever was needed with the character, and then called getc() again, which cleared stdin of any remaining characters. This "flushed" the system so that I wouldn't get that errant newline character again.

Honestly, I was never that good with C to begin with. Learning takes time, I guess.
>> No. 13
I am of the belief that, in general, programs should not be interactive. Interactivity should be limited to the command interpreter when possible. If your program is asking "please enter a number", you should use commandline arguments or configuration files instead.

Making programs non-interactive avoids the issue you are talking about. Also makes it easier to integrate with and leverage other programs in the system. Also makes it easier to script.

Avoid interactivity.
>> No. 14
I am of the belief that, in general, programs should not be interactive. Interactivity should be limited to the command interpreter when possible. If your program is asking "please enter a number", you should use commandline arguments or configuration files instead.

Making programs non-interactive avoids the issue you are talking about. Also makes it easier to integrate with and leverage other programs in the system. Also makes it easier to script.

Avoid interactivity.
>> No. 15
Some programs are meant to be interactive.
If you want to automate something, interactivity isn't a good idea (stick with pipes, flags, and configuration files). Besides, interactivity is better accomplished with a GUI, unless you're playing a good old text adventure. ;)
I use fread() and fgets() for pipes. If you want to flush the buffer of any newlines, though, fflush() is dedicated to that.
>> No. 17
I don't use strictly C (I work mostly with C++, which has the C library... well you guys know) Anyway, I agree with >>13 that if your writing for consoles the program, generally speaking shouldn't be interactive, however this is only for more professional pieces of work. If your making something for yourself, go crazy.

OH and i dunno about C, but C++ has something specifically for that called sync(), i've dealt with the same problem in the past.
>> No. 190
https://trac.transmissionbt.com/browser/trunk/libtransmission/ggets.c?rev=5429
What you want is a fggets() function. This works just like fgets() but takes a pointer to a string (a char** or the & operator on a char*) and points it to a buffer that's automatically resized to fit the line. The nice thing is you never have to worry about buffer overflows or lines that are too long and you always get a full line or EOF. If you run out of memory, it just returns an error code and doesn't crash your program. It also replaces the newline with a null character and removes it from the stream, just like you want. When you're done with the string or you want to get another line, just pass it to free().

Or if you're on Linux you could use getline(), which works pretty much the same way.
>> No. 199
What may be of interest to you is readline or editline.


Delete post []
Password  
Report post
Reason  




Inter*Chan Imageboard Top List