Thread: Garbage appearing

  1. #1
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187

    Garbage appearing

    Hey guys,
    I'm sending a phrase through a named pipe and my server receives it well but when printing the phrase he prints it well but then prints some garbage aswell

    This is my client :

    Code:
    #include <sys/types.h>#include <sys/stat.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    
    
    #define MAX_LINE 80
    
    
    int main(int argc, char** argv) {
    	char line[MAX_LINE];
    	if(argc < 2) printf("Invocação do programa cliente mal realizada!\n");
    	else if(strncmp(argv[1],"add",strlen(argv[1]))==0)
     	 	{
     		  	if(argc != 2) printf("O comando add foi mal invocado.\n Exemplo: ./cliente add n");
       		  	else
       		 	{	int pipe = open("trabfifo", O_WRONLY);
        				// get a line to send
    				printf("Enter line: ");
    				fgets(line, MAX_LINE, stdin);
    				line[strlen(line)-1] = '\0';	
    				// actually write out the data and close the pipe
    				write(pipe, line, strlen(line));
       		 	}
    		}	  
    		else if(strncmp(argv[1],"help",strlen(argv[1])) == 0)
      		{	
      			ajuda();  		
      		} 	
      		else printf("Comando Inexistente.\n");
    		return 1;
    }
    And now my server code :

    Code:
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    
    
    #define MAX_LINE 80
    
    
    int main(int argc, char** argv) {
       int pipe;
       int n;
       char line[MAX_LINE];
       mkfifo("trabfifo",0646);
    
    
       // abrir um named pipe
       pipe = open("trabfifo", O_RDONLY);
       // ler a informação e fechar o pipe
       n = read(pipe, line, strlen(line));
       printf("%s \n",line);
       printf("%d \n",n);
       close(pipe);
       return 0;
    }
    After sending the phrase 'ola' the result is the following :

    ola
    �
    4

    Why is that garbage appearing in the mid ?

    Thanks.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Note: I have never used pipe functions; my answer is a good guess on the problem.

    Old code
    Code:
    char line[MAX_LINE];
    Code:
    n = read(pipe, line, strlen(line));
    The changes I suggest

    Code:
    char line[MAX_LINE+1]={0};
    Code:
    n = read(pipe, line, MAX_LINE);
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Quote Originally Posted by stahta01 View Post
    Note: I have never used pipe functions; my answer is a good guess on the problem.

    Old code
    Code:
    char line[MAX_LINE];
    Code:
    n = read(pipe, line, strlen(line));
    The changes I suggest

    Code:
    char line[MAX_LINE+1]={0};
    Code:
    n = read(pipe, line, MAX_LINE);
    Tim S.
    Changed it and worked.
    Thanks very much Tim!

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well I know the read function just buffers whatever is actually there, where it's reading from. Unless your data has terminated strings, you will need to terminate them yourself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. include <iostream> appearing red
    By Tachimazu in forum C++ Programming
    Replies: 3
    Last Post: 06-21-2009, 10:59 PM
  2. Menu not appearing
    By P4R4N01D in forum Windows Programming
    Replies: 4
    Last Post: 02-13-2009, 10:27 PM
  3. Unwanted characters keep appearing
    By sundeeptuteja in forum C++ Programming
    Replies: 10
    Last Post: 03-01-2003, 12:58 AM
  4. CDialog actions after appearing
    By phil_drew in forum Windows Programming
    Replies: 0
    Last Post: 12-06-2002, 07:13 AM
  5. Text string not appearing - RH problem?
    By westm2000 in forum C Programming
    Replies: 1
    Last Post: 10-05-2002, 07:49 PM