read and write stdin/stdout in unix

Hi,
i am using the below program to read from the standard input or to write to standard out put.

i know that using highlevel functions this can be done better than what i have done here.

i just want to know is there any other method by which i find the exact number of characters ( this will vary in each iteration ) , and write that to STDOUT.

this process i want to terminate with a press of "q".

does read syscall stores the null at the end of string, i have not found that in debug mode.

below is the sample code but its has got the problem of reading and writing garbages.

#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <stdio.h>
#include<string.h>

int main( void )
{
        char buf[20];

        while( 1 )
        {
                       read(STDIN_FILENO , buf ,  sizeof( buf ));
                       write(STDOUT_FILENO, buf , strlen( buf ));

                if(!strcmp(buf,"q\n"))
                        break;
        }

        return 0;
}

The read() call is not going to terminate the input and make it a string.

You could do it like this:

size_t bytes_read;
   .
   .
   .
bytes_read = read( STDIN_FILENO, buf, sizeof( buf ) - 1 );
if ( bytes_read >= 0 )
{
    buf[ bytes_read ] = '\0';
}
   .
   .
   .

That might work. Or you might have problems with input buffering, and instead of reading strings your program reads one character at a time, with each keypress getting read as you hit the key.

      1 #include<unistd.h>
      2 #include<fcntl.h>
      3 int main( void )
      4 {
      5         int fd, fd1;
      6         fd = open("MyFile.txt",O_RDWR|O_CREAT);
      7         close(1);
      8         if( ( fd1 = dup(fd))!= 1)
      9                 printf("Its not set \n");
     10         printf("fd1 = %d \n", fd1);
     11         close(fd);
     12         printf("hello");// this will be printed in MyFile.txt .
     13         return 0;
     14 }

i am using the ablove program to make MyFile behave as standard out put.
i tries in two ways

  1. fd = open("MyFile.txt",O_RDWR|O_CREAT);
  2. fd = open("MyFile",O_RDWR|O_CREAT);
    when i use 1 its not writting anything into file.
    but if i use 2 its writting the contents.

whats the problem with .txt

The program actually works for me despite its many errors, but creates the file with 000 permissions which means you can't read it once its done. You should also be using the dup2 call instead of just hoping the next file you open becomes FD 1. I'd make these revisions:

#include <unistd.h>
#include <fcntl.h>
// Need this for printf
#include <stdio.h>

int main( void )
{
        int fd = open("MyFile.txt",O_RDWR|O_CREAT, 0666);

        if(fd < 0)
        {
                perror("Couldn't open MyFile.txt");
                return(1);
        }

        if(dup2(fd, STDOUT_FILENO) < 0)
        {
                perror("Couldn't duplicate file");
                return(1);
        }

        close(fd);

        printf("hello");// this will be printed in MyFile.txt .
        return 0;
}