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.
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.
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;
}