Code a C file, following these instructions:
This lab is about getting the input from a file. Let’s assume the words are provided in one file, passed as an argument to your program. The names of the files are provided as arguments to your program (i.e., they are copied by the shell in the argv variable), and each file is a text file with lots of words.
Open the manual page for open() system call and add to your code the header files required to use open(), using the include preprocessor statement.
Use the open() system call to open the file passed to your program in argv[1] for reading. What flag should you pass to open(), to open the file in read-only mode?
Open the manual page for read() system call and add to your code the header files needed for read().
Use the read() system call to read the characters from the file. What parameters does read() take, and what value does it return? Repeat, if necessary the call to read() until all characters are read from the input file.
Use printf() to output to the terminal the text just read from the input file.
Open the manual page for close() system call and determine which header file you should include in your code to call close().
Use the close() system call to close the file.
---
Thank you greatly! I appreciate the help!
Header file for file system calls: fcntl.h
open() system call: Opens file in different modes
int open (const char* Path, int flags [, int mode ]);
Arguments:
Path: Path to file
flags: modes to open file
O_RDONLY: read
only,
O_WRONLY: write
only,
O_RDWR: read and
write,
O_CREAT: create
file if it doesn’t exist,
O_EXCL: prevent
creation if it already exists
read(): From the file indicated by the file descriptor , the read() function reads bytes of input into the memory area indicated by buffer. A successful read() updates the access time for the file.
size_t read (int fd, void* buf, size_t cnt);
Arguments:
fd: file descripter
buf: buffer to read data from
cnt: length of buffer
Returns: bytes read
close(): closes the opened file
Arguments:
fd: file descriptor
Here is the code:
// File Processing
#include <stdio.h>
#include <fcntl.h> // header file for open, read and close system calls
int main(int argc, char *argv[]) // argc, argv for reading command line arguments
{
int fd, sz;
char c[255];
/**
Opens file in different modes
Arguments:
Path: Path to file
flags: modes to open file
O_RDONLY: read only,
O_WRONLY: write only,
O_RDWR: read and write,
O_CREAT: create file if it doesn’t exist,
O_EXCL: prevent creation if it already exists
*/
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
perror("r1");
exit(1);
}
/**
From the file indicated by the file descriptor , the read() function reads bytes of input into the memory area indicated by buffer. A successful read() updates the access time for the file.
Arguments:
fd: file descripter
buf: buffer to read data from
cnt: length of buffer
Returns: bytes read
*/
sz = read(fd, c, 255);
c[sz] = '\0';
printf("File Contents: \n%s\n", c);
/**
Closes the opened file
Arguments:
fd: File Descriptor
*/
close(fd);
return 0;
}
file1.txt:
Hey everyone,
How are you all?
This is an example of file reading.
Output:
Code Screenshot:
Get Answers For Free
Most questions answered within 1 hours.