I've been stuck on this for hours because I really don't understand file descriptors and how successfully print a file to one.


Basically, I want to be able to open a file and then echo its contents a character at a time to a socket file descriptor. The code below will hopefully help you to understand, and I've made a start on what I think is correct. I just can't figure out what to include in the while loop, although I think it is through use of char *fgets and int fputs. Any help whatsoever will be greatly appreciated.


Code:
void echoFile(int fd, char *filename) {


    // file pointer
    FILE* fp;
    
    // open 'filename' and assign the pointer
    if((fp = fopen(filename, "r")) == NULL) {
            fprintf(stderr, "The file specified does not exist\n");
    }
    
    //while loop to read each character from 'filename' to the file descriptor
    while(getc(fp) != EOF) {
  
    }
}