Just another example...
#include <unistd.h> #include <fcntl.h> #include <stdio.h> #include <errno.h> int main(int argc, char const *argv[]) { char fileName[20]; int fileFound = 0; // Get the filename from the user: int fileNameLength = read(0,fileName,19); // We need to get rid of the new line character caused by terminal // Replace the new line character with \0 fileName[fileNameLength-1] = fileName[fileNameLength]; printf("You want to see the contents of: %s\n", fileName); // open the file: int fd = 0; fd = open(fileName,O_RDONLY); if (fd == -1) { // Something went wrong, perhaps no such file: perror(NULL); printf("%s\n", fileName); } else { // Read until the read method returns 0 bytes. char buf[20]; int numBytesRead = read(fd, buf, 20); while (numBytesRead) { write(1, buf, numBytesRead); numBytesRead = read(fd, buf, 20); } close(fd); } puts(""); }