Results 1 to 10 of 12
Hi everyone!
im looking for a little help for an assignment.
im using the fgets() function to stored a string in an array of size 20...it works fine, however i ...
- 09-03-2007 #1
fgets problem with while loop
Hi everyone!
im looking for a little help for an assignment.
im using the fgets() function to stored a string in an array of size 20...it works fine, however i need it to continually ask for input, and so i used a while loop.
the problem here is that if an input greater than 20 characters in entered then fgets will store the first 20 and continue on, but when it loops back around it will clear the array and insert the excess characters and continue until it runs out of input characters.
while (trigger != 0)
{
if (strlen(fgets(isbnInput, 20, stdin)) != 1 )
{
/* ive removed excess code here because its irrelevant */
}
else
{
trigger = 0;
}
}
any help would be greatly appreciated thanks!!
- 09-04-2007 #2
Righto.
The problem here is that stdin is actually a stream, and more specifically, a buffered stream. So when you run fgets() on any buffered file stream, what actually happens is:
1. fgets() checks to see if there is currently any data in the file stream's buffer.
2a. If there is data in the buffer, fgets() returns the first 19 characters of it (or less if there are less than 19 characters).
2b. If there is no data in the buffer, more data is requested from the stream. In the case of a file, more of the file is read. In the case of stdin, the user is prompted for more input. fgets() then goes to step 2a.
So if you want to force a brand new set of data on each read, you need to clear all of the data in the buffer. This is called "flushing", and there is a function called fflush(FILE *) that will flush any output buffer.
Unfortunately, in this case, we are looking at an input buffer. There is no function in the standard C Library for this, and I do not believe that there is one in glibc. I am not sure of the best way to write such a function, but hopefully you will learn a bit about how these streams work, and can start looking for a solution on your own.DISTRO=Arch
Registered Linux User #388732
- 09-04-2007 #3
oh wow...ic ic
thanks alot for replying so quickly
ill get working on that right away...cheers...appreciate it...
I also found a website that may help to accomplish this so ill leave it here for any others that need it :P
Common C Programming Errors
thanks again
- 09-04-2007 #4
and another clearer one...same coding...just easier to find on the page since its specifically made to demo the clearing of an input buffer
Cprogramming.com: FAQ
- 09-04-2007 #5
If I understand what you're doing, normally I store what I get from fgets in a buffer, then use sscanf to get whatever I need out of it. So I do like
Code:while (fgets(buffer, sizeof(buffer), stdin) != EOF) { do whatever to buffer }
AFAIK you don't even really even need to put != EOF, but I do it for clarity. Once you have that buffer read, you can do strlen on it, or strcmp, whatever you want.
- 09-05-2007 #6
thanks for that
unfortunately that assignmnet specs reads that i need to erase unread characters i.e. characters that exceeed the 20 char array size (i assume) so i guess clearing the input buffer is the way to go here...thx
- 09-16-2007 #7
ok...just got a solution for this...
basically the way to clear the buffer is to read from it...
so...
that should work...seems to be working for me anywayCode:char temp; do scanf("%c", &temp); while (temp != '\n');
- 09-16-2007 #8
Sorry forgot about this thread... though you are not posting the whole problem, you might want to look into fflush() if it fits for this scenario. I find myself using it in situations with curses and getch() on Solaris.
-- EDIT --
Oops didn't notice that's already been mentioned. Either way, you've found your solution
- 09-16-2007 #9
Actually fflush(stdin) worked...but compiling it at uni produced alot of errors because its 'overhead expensive' as someone said...so yeah just an alternative solution
- 09-17-2007 #10Just Joined!
- Join Date
- Sep 2007
- Posts
- 1
I have got the same problem as yours. When fflush is used on the local machine, everything is working....no error message, on warrring message. However, when I submit it, the testing program will be stopped.....
if you can find something to fix this problem, please post it here...
cheers!


Reply With Quote