NO

Author Topic: (SOLVED) _pipe Usage (Vista)  (Read 2980 times)

Juli

  • Guest
(SOLVED) _pipe Usage (Vista)
« on: January 21, 2013, 02:26:11 PM »
Hello Forum :)

I m trying to write a console program
wich is able to "pipe" input.

It should be able to accept input from commands like "ls / dir / echo ..".

The problem is i allways get "eof".
( For example by using "dir | myprg.exe" )

Here is my code:
Code: [Select]
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <io.h>
#include <fcntl.h>

#define READ 0
#define WRITE 1

int main(int argc, char **argv)
{
int PipeX[2];
int buff;

// Create a Read-Pipe
if (_pipe( PipeX, sizeof(buff), _O_TEXT ) == -1)
printf ( "error creating pipe\nn" );
else printf ( "pipe: created\n" );
_close( PipeX[WRITE]);

// Read from Pipe
int x;
x = _read( PipeX[READ], (char *) &buff, sizeof (int)  );
printf ( "read: %d \n", x);
printf ( "received: %d ", buff);
_close( PipeX[READ]);

if ( x == -1 ) printf ("error reading from pipe" );
if ( x > 0 ) printf ("reading from pipe should have worked" );
if ( x == 0 ) printf ("eof ");


return 0;
}


What am i doing wrong ?

 ;D Found it

This Snipet does what i aimed for

Code: [Select]
#include <windows.h>
#include <stdio.h>
#include <unistd.h> // STDIN_FILENO

int main(int argc, char **argv)
{
int fdin = 0;
int test;
int buffsize = 3;
char buffer[ buffsize ];

memset( buffer, '\0', buffsize );
if( _dup2( STDIN_FILENO, fdin ) < 0 )
{
perror("ERROR: _dup2");
return 0;
}

while( ( test = read( fdin, buffer, buffsize ) ) > 0 )
{
printf( "<%s>", buffer );
memset( buffer, '\0', buffsize );
}
if( test < 0 ) perror( "read(  )" ); 
 
close( fdin );

system("pause");
return 0;
}
« Last Edit: January 21, 2013, 06:56:04 PM by Juli »