Jul 24, 2008

Error Using Fgets And Strcmp

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

free web hosting

Error Using Fgets And Strcmp

kvarnerexpress
G'day D'day, first time posting and even my first time at the site (found after a quick google) so i hope i've followed the correct protocol. I'm stuck using borland C in Windows at home and i've come across a simple problem. My knowledge of C spans from the basics up to Stacks, Queues, Lists, Linked/Double versions, Binary tree and hash tables but its the simple things that always get me stumped.

The issue i'm having is that i use a fgets to recieve and store a string, followed by a strcmp with the saved string and "generic preset command" resulting in failed conditions. Right now the program i'm writing is a bit large so i've just written a small sample one to illustrate the problem:

Code:

PHP Code:
CODE

char array[20];
   printf("Enter a string\n\n");
   fgets(array,1024,stdin);

   if(strcmp(array,"bob the builder")==0)
    {
       printf("%s is a valid command",array);
   }
   else
   {
       printf("Error: Unknown Command, try again");
   }  


It will always result in the error part of the loop and i'm fairly certain that the issue arises from fgets recieving one additional character, but i can't for the life of me remember how to go about it. I had a quick look around the FAQ (Which i applaud the posters for as it resolved many issues i've had) but couldn't find anything regarding this. Once again, awesome site full of answers to questions and issues i had a year ago with my comp sci course

 

 

 


Reply

methane
QUOTE(kvarnerexpress @ Feb 20 2006, 04:27 AM) *

G'day D'day, first time posting and even my first time at the site (found after a quick google) so i hope i've followed the correct protocol. I'm stuck using borland C in Windows at home and i've come across a simple problem. My knowledge of C spans from the basics up to Stacks, Queues, Lists, Linked/Double versions, Binary tree and hash tables but its the simple things that always get me stumped.

The issue i'm having is that i use a fgets to recieve and store a string, followed by a strcmp with the saved string and "generic preset command" resulting in failed conditions. Right now the program i'm writing is a bit large so i've just written a small sample one to illustrate the problem:

Code:

PHP Code:
CODE

char array[20];
   printf("Enter a string\n\n");
   fgets(array,1024,stdin);

   if(strcmp(array,"bob the builder")==0)
    {
       printf("%s is a valid command",array);
   }
   else
   {
       printf("Error: Unknown Command, try again");
   }  


It will always result in the error part of the loop and i'm fairly certain that the issue arises from fgets recieving one additional character, but i can't for the life of me remember how to go about it. I had a quick look around the FAQ (Which i applaud the posters for as it resolved many issues i've had) but couldn't find anything regarding this. Once again, awesome site full of answers to questions and issues i had a year ago with my comp sci course



What you said is correct, there is an additional character receive from fget. That is the newline character. Do you remember what is the last key when you input the command? That is the 'Enter' Key. So you have to include the newline character in your compare string. i.e. the compare string should be 'bob the builder\n' instead of 'bob the builder'

CODE

char array[20];
   printf("Enter a string\n\n");
   fgets(array,1024,stdin);

   if(strcmp(array,"bob the builder\n")==0)
    {
       printf("%s is a valid command",array);
   }
   else
   {
       printf("Error: Unknown Command, try again");
   }  

Actually, this problem can be solved if you run it in debug mode. Try to watch the variable and compare the strings manaully, you will found that 2 strings are somehow different.
Please also be noted that your array size is only 20 while fgets read in 1024 bytes, it may cause memory error if you input string is longer than 19.

 

 

 


Reply

jibnet
I just read you problem.. and it is clear that it is happening because the '\n' character is also stored in the string.. now all u need to do is remove it from the string. to do it follow this:

CODE
int i;
i=strlen(array);   /*Get the length of the string */

/*Note: if for example we get the length of  "india", we would would get the result as 5. also note that the last character of "india" is 'a' and it would be stored in array[4], since the array count starts from zero. and also array[5]='\0'(i.e. String terminating character "NULL").*/

i--;        /*(Remove a character from the string)*/
array[i]='\0';

/* The '\n' is removed now.. ENJOY*/


for more info ... mail me at jibranbhat@gmail.com

Notice from BuffaloHELP:
Knowing how to use CODE bbcode is part of Trap17 forum rule.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:


Searching Video's for error, fgets, strcmp
advertisement



Error Using Fgets And Strcmp



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE