• Advertisement
Sign in to follow this  

Problem with strings under Linux

This topic is 4239 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

Hi, whenever I am working with C-style strings under Linux ( and I can remember once under Solaris ) I tend to have some problems I do not encounter while working under Windows with MS compiler. From time to time I get garbage characters at the end of my strings. Here is some code I posted in the general forums :
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); 
             } 
         } 
     }    
 } 
I thought it was fine and people there seemed to agree but sometimes printf is printing some garbage characters at the end of it's output. This usually happens when temp == NULL while it wasn't NULL in the loop before.

Share this post


Link to post
Share on other sites
Advertisement
Use:

char *temp = NULL;

You're not initializing your variable, so it can point anywhere. Pointers are *not* by default set to NULL (they are in Win32/MSVC). Your compiler should warn you about this. If you're using GCC then set the -Wall compiler flag. Then it'll report things like this as warnings.

EDIT: Wait.... I see you are setting it.

EDIT 2: I see you are not checking the return value of fgets(). Maybe something like:


void sub(char *desc, char *text) {

char string[255];

char *temp;



while (fgets(string, 255, stdin)) {

temp = strstr(string, desc);



if (temp == NULL) {

printf("%s", string);

}

else {

printf("%s%s", text, temp);

}

}

}

Share this post


Link to post
Share on other sites
I think your string array should be 1 byte larger than the size you read. Eg. 256 bytes

Share this post


Link to post
Share on other sites
fgets will only read n-1 bytes, reserving the n'th byte for the \0. Thta's at least what the glibc documentation says.

Share this post


Link to post
Share on other sites
I would suggest a rewrite that does not use fgets(), since that function, while part of the the C runtime library, doesn't work so well with stdin.


For reading the actual data from the console:


#include <sys/time.h>
#include <sys/select.h>

#define STDIN 0

void ReadFromConsole(char* text)
{
fd_set set;
struct timeval tv = { 0, 0 };

FD_ZERO(&set);

FS_SET(STDIN, &set, );

//replace with final NULL with &tv to have non-blocking I/O
select(STDIN + 1, &set, NULL, NULL, NULL);

if(FS_ISSET(STDIN, &set))
{
read(STDIN, text, 255);
}

}



EDIT:

Also try doing a memset() on the 255 char array every time and see if somehow a non-NULL termination is causing the problem.

Share this post


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

  • Advertisement