473,326 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

fgets function problem

What i've written below is a much simplified version of my program but it has the exact same problem (didn't want to bore people with long code).
I wrote the printf's appropriate to what the code does or doesn't do.
it doesn't take the fgets (in the function) in the second while loop but never the first, and i want it to take a string in the first.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int this_is_function()
{
char anotherBuffer[255];
char test2[] = "win";

printf("This fgets doesn't catch first time.\n>> ");
fgets(anotherBuffer, 4, stdin);

printf("%s\n", anotherBuffer);

if (strcmp(test2,anotherBuffer) == 0){
printf("success");
return 1;
}

return 0;
}

int main(void)
{
char *buffer;
char test1[] = "hello";

buffer = (char*)malloc(255);

printf("If i enter some input here.\n>> ");
fgets(buffer, 6, stdin);

printf("%s\n", buffer);

if (strcmp(buffer,test1) == 0){
while (this_is_function() == 0)
printf("Error");
}

free(buffer);
}

any ideas? thanks in advance
Sep 12 '09 #1
3 6343
Savage
1,764 Expert 1GB
@ocli5568
There is probably a '\n' left in input stream,which is read in first loop iteration causing fgets to stop receiving input from the stream.


So you will need to check to see if the input stream is empty before you use fgets,if it is not you'll need to empty it yourself ,something like:

Expand|Select|Wrap|Line Numbers
  1. int this_is_function()
  2. {
  3.     char anotherBuffer[255];
  4.     char test2[] = "win";
  5.  
  6.     printf("This fgets doesn't catch first time.\n>> ");
  7.  
  8.         while(!feof(stdin))
  9.         {
  10.             getchar();
  11.         }
  12.  
  13.     fgets(anotherBuffer, 4, stdin);
  14.  
  15.     printf("%s\n", anotherBuffer);
  16.  
  17.     if (strcmp(test2,anotherBuffer) == 0){
  18.         printf("success");
  19.         return 1;
  20.     }
  21.  
  22.     return 0;
  23. }
NOTE:Usually there will be only '\n' left in stream so you could change the while loop statement for an if statement.
Sep 12 '09 #2
JosAH
11,448 Expert 8TB
@ocli5568
In main when you type in six characters and press the enter key more than six characters are present in the system's input buffer but at maximum only six of them are read. The others are left there for a next read (scanf). Even worse, Microsoft in all its glorious wisdom had decided that the enter key stuffs two characters in your input buffer: \r and \n. If even one of them is left in the input buffer your reading goes berzerk. You have to anticipate on that.

kind regards,

Jos
Sep 12 '09 #3
thanks so much for all the advice. although savage i tried implementing your version, it seemed to get stuck on a while loop and i don't understand enough about the error functions to go about fixing anything there. but i tried initializing my test function as what it is, adding a '\n' to the end of it and that cleared up the problem (using both of your help, again thankyou).
would that fix the problem consistently? because it seems like a flimsy quick fix.
Sep 12 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
26
by: Peter Mount | last post by:
Hello What's the syntax for using fgets() to store a string in memory? I understand that fgets() can solve the problem of storing a string that has more characters than the size of the declared...
0
by: Paul Hsieh | last post by:
"Paul D. Boyle" <boyle@laue.chem.ncsu.edu> wrote: > There was a recent thread in this group which talked about the > shortcomings of fgets(). I decided to try my hand at writing a > replacement...
20
by: TTroy | last post by:
Hello, I have found some peculiar behaviour in the fgets runtime library function for my compiler/OS/platform (Dev C++/XP/P4) - making a C console program (which runs in a CMD.exe shell). The...
1
by: Aomighty | last post by:
Hi, I've been creating a simple calculator program that asks whether you want to add, subtract, multiply or divide, asks you for input, and then performs the calculation. Things seem to be going...
11
by: santosh | last post by:
Hi, A book that I'm currently using notes that the fgets() function does not return until Return is pressed or an EOF or other error is encountered. It then at most (in the absence of...
32
by: FireHead | last post by:
Hello C World & Fanatics I am trying replace fgets and provide a equavivalant function of BufferedInputReader::readLine. I am calling this readLine function as get_Stream. In the line 4 where...
285
by: Sheth Raxit | last post by:
Machine 1 : bash-3.00$ uname -a SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R bash-3.00$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/...
26
by: Bill Cunningham | last post by:
I was talking with someone about fgets and he said that fgets puts the \n in a string but not \0. I decided to test this assumption because my documentation didn't say if fgets put \0 after a...
12
by: Krumble Bunk | last post by:
Hi all, Having some trouble with a seemingly-simple task. Need to have a basic CLI functionality as per: prompt <- \n prompt <- \n promptquit <- should quit as per my snippet, but...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.