I need to read in a file, then print it out to STDOUT but double spaced. What I have so far is:
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define BUFSZ 1024
int main(int argc, char* argv[]){
int n, fdin;
char buf[BUFSZ];
if ((fdin=open(argv[1],O_RDONLY))<0){
perror(argv[1]);
exit(-1);
}
while(( n = read(fdin, buf, BUFSZ))>0){
if(write(STDOUT_FILENO,buf,n) != n){
fprintf(stderr, "Write Error\n");
exit(-1);
}
printf("\n");
}
close(fdin);
return(0);
}
I’m new to C and don’t know how I could implement the \n into the code, my printf(“\n”) is useless in trying to double space the entire file’s contents. I think I have to add the \n into the read? But I’m not entirely sure if that’s correct or how to do it.
read()does not stop reading when it encounters a new-line character so the algorithm in the posted code could easily miss writing a blank line as theread()could read more than one line.A simpler approach would use
fgets():