Jump to content
  • Advertisement
Sign in to follow this  
Bleakcabal

C, strstr, strings question

This topic is 4522 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is a C fonction I have programmed and written here in a simplified manner :
void sub(char *desc, char *text) { 
     char string[255]; 
     char *temp; 
         
     while (!feof(stdin)) { 
            fgets(string, 255, stdin); 
 
         if (!feof(stdin)) { 
             temp = strstr(string, desc); 
 
             if (temp == NULL) { 
                 printf("%s", string); 
             }    
             else {    
                 printf("%s%s", text, temp); 
             } 
         } 
     }    
 } 
What I am getting is after a line that goes into the else clause ( meaning strstr(string, desc) wasn't null ) the following line there are some garbage characters which are printed, even if it only prints the string which should'nt be affected by strstr. I took strstr usage's ( returning it to a char * ) directly from an example on the net. What am I doing wrong here ?

Share this post


Link to post
Share on other sites
Advertisement
What characters exactly are you referring to with "garbage characters"? The newline/carriage return stuff will show up as odd characters.

Share this post


Link to post
Share on other sites
I can't see anything wrong with the code. Your garbage characters are probably in the string recieved from fgets. Like Evil mentioned, they're probably non printing characters like newline and CR. Try manually filling string with some test data and see if that fixes it.

Share this post


Link to post
Share on other sites
Quote:
Original post by perfectly_dark
I can't see anything wrong with the code. Your garbage characters are probably in the string recieved from fgets. Like Evil mentioned, they're probably non printing characters like newline and CR. Try manually filling string with some test data and see if that fixes it.


I'm doing this under linux, every time I work with strings under Linux I tend to get some crap at the end of my strings every now and then.

My guess is that some library or whatnot isn't implemented the same as under Windows.

Share this post


Link to post
Share on other sites
Linux and Windows use different characters for a new line. I think Windows uses a Carriage Return Line Feed whereas Unix derivatives use just a Line Feed. So if you're reading a file or stream that uses CRLF as a newline using the Linux libraries, you'll see the Carriage Return character.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bleakcabal
Shouldn't printf stop at the carriage return, end of line characters, thus, I should'nt be able to see them ?


No. printf() prints everything you tell it to, up to the first '\0'.

Think about it. What do you expect printf("\n\n") to print? What about printf("%s\n", "\n")?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!