Advertisement

Problem with strings under Linux

Started by May 31, 2006 04:48 PM
3 comments, last by Figgles 18 years, 4 months ago
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.
WHO DO THEYTHINK THEY'REFOOLING : YOU ?
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);              }      }     }

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

Advertisement
I think your string array should be 1 byte larger than the size you read. Eg. 256 bytes
fgets will only read n-1 bytes, reserving the n'th byte for the \0. Thta's at least what the glibc documentation says.

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

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 0void 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.

This topic is closed to new replies.

Advertisement