popen problem

This is a discussion on popen problem within the C Programming forums, part of the General Programming Boards category; Hi, I have been learning about pipes in C recently. Here is an example of some code that works fine: ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    popen problem

    Hi,

    I have been learning about pipes in C recently. Here is an example of some code that works fine:

    Code:
    // some lines are omitted
    	switch(fork()) {
    		case -1:
    
    		perror("Fork");
    		exit(1);
    
    		case 0:
    
    		dup2(p[1], fileno(stdout));
    		close(p[0]);
    		close(p[1]);
    		execl("/usr/bin/last", "last", 0);
    
    		_exit(0);
    
    		default:
    
    		printf("parent\n");
    
    		dup2(p[0], fileno(stdin));
    		close(p[1]);
    		close(p[0]);
    
    		execl("/bin/sort", "sort", 0);
    	}
    Now this does exactly what it is supposed to do, last | sort , I'm trying to do all this with popen instead. Here is my attempt at that.
    Code:
    int main(void)
    {
    	FILE *fr, *fw;
    	int i, j=0;
    
    	char buffer[BUFSIZE];
    
    	fr = popen("last", "r");
    	fw = popen("sort", "w");
    
    	while (fgets(buffer, sizeof(buffer), fr))
    		fprintf(fw, "%s\n", buffer);
    
    	pclose(fr);
    	pclose(fw);
    	
    	return 0;
    }
    This almost works perfectly, except I get 10000 blank lines printing to stdout first. I know it's 10000 because I through in a counter at one point. After the blank lines it outputs semi-properly. every 10 lines or so a line that's not sorted properly appears. Thanks for any help/advice on this.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    > fprintf(fw, "%s\n", buffer);
    buffer already contains a newline from the fgets

    Use fputs in place of fprintf
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    25
    thanks, it actually works fine with fprintf after removing the \n
    I will try with fputs as well

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  4. Pipe problem, popen(), pipe().
    By apacz in forum C Programming
    Replies: 7
    Last Post: 06-08-2006, 01:55 AM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21