As part of a question where I have to pipe processes together, I have to have one process simply send the sequence of integers 1, 2 .....10000 out of standard output. The problem I have is that I can not get it to work decently in any way shape or form.
I have been writing a (very) simple program to try out different methods. When I write to the stdout directly, I get a formless list of 10000 numbers. When I try to write the string of numbers to a file, the text editor freezes when I attempt to open it. The program is as follows:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char* argv[]) {
char *arg[]= {"/home/eric/Documents/pr3/test.txt"};
int i;
int fp = open(arg[0], O_WRONLY);
char tmp[12]={0x0};
for(i=1; i<=10000; i++){
sprintf(tmp,"%11d", i);
write(fp, &tmp, sizeof(tmp));
}
}
I have no idea why that happens and would very much appreciate some help.
Thank you.