Question

Code a C file, following these instructions: This lab is about getting the input from a...

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.

  1. 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.

  2. 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?

  3. Open the manual page for read() system call and add to your code the header files needed for read().

  4. 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.

  5. Use printf() to output to the terminal the text just read from the input file.

  6. 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!

Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In the book_store.cpp file, add the code for the process_orders method. The argument that is passed...
In the book_store.cpp file, add the code for the process_orders method. The argument that is passed to this method is the name of a file that will be read in the method. Each line in the file contains an order number (integer), isbn number (character array), and amount ordered (integer). If an order number is on a line, it is guaranteed that there will be an isbn number and amount ordered to go along with it. Create an input file...
Convert the following C program to C++. More instructions follow the code. #include <stdio.h> #include <stdlib.h>...
Convert the following C program to C++. More instructions follow the code. #include <stdio.h> #include <stdlib.h> #define SIZE 5 int main(int argc, char *argv[]) {    int numerator = 25;    int denominator = 10;    int i = 0;    /*    You can assume the files opened correctly and the    correct number of of command-line arguments were    entered.    */    FILE * inPut = fopen(argv[1], "r");    FILE * outPut = fopen(argv[2], "w");    float result =...
Please complete the following code for challenge.c in C according to the instructions in the comments....
Please complete the following code for challenge.c in C according to the instructions in the comments. Further instructions are below in INSTRUCTIONS.txt challenge.c #include "challenge.h" // goal: fork the process and have the child execute a process // param argv: the argument vector for the process to be executed // assumptions: // the first argument of argv is the file name of the executable // argv is null terminated // // TODO: complete the function // fork // exec (child),...
contents  Please use the DFT to convert the given sound file (wav) signal from time...
contents  Please use the DFT to convert the given sound file (wav) signal from time domain to frequency domain and output vibration. The text size of the size and phase size  Program requirements Requires the use of argc and argv Enter XXX.exe input.wav magnitude.txt phase.txt in cmd It can be executed. The input is the audio file (input.wav), and the output is amplitude and phase. Small text file (magnitude.txt/phase.txt) “ “rb” is used for the read mode and...
1.      Create 100 text files automatically; in each file write a random number from 1 to 10....
1.      Create 100 text files automatically; in each file write a random number from 1 to 10. Use outputstreams (fileoutputstream, buffredwriter….) 2.      Read the content of the 100 files and combine them into a 1 single file. 3.      Write java code to do the following: a.      To write the following text into a text file EEEESAAA@23SDCFSAWERF%WASDFGHWERTRQW b.      Read the file using a java program c.      Find how many D’s in the file d.      Extract the text between the @ and the # 1. Create 100 text files...
Write an assembly program that reads characters from standard input until the “end of file” is...
Write an assembly program that reads characters from standard input until the “end of file” is reached. The input provided to the program contains A, C, T, and G characters. The file also may have new line characters (ASCII code 10 decimal), which should be skipped/ignored. The program then must print the count for each character. You can assume (in this whole assignment) that the input doe not contain any other kinds of characters.  the X86 assembly program that simply counts...
C# Reading from Files Write a program to open a text file containing information about buildings....
C# Reading from Files Write a program to open a text file containing information about buildings. The program must display all the buildings in the file and then find the lowest cost building based on the cost per square foot. Format for one building: <building name> <size> sqft $<cost> Example: Allgood Hall 35000 sqft $8,250,099.75 Create the text file and add three or more of your favorite buildings.
C# Reading from Files Write a program to open a text file containing information about buildings....
C# Reading from Files Write a program to open a text file containing information about buildings. The program must display all the buildings in the file and then find the lowest cost building based on the cost per square foot. Format for one building: <building name> <size> sqft $<cost> Example: Allgood Hall 35000 sqft $8,250,099.75 Create the text file and add three or more of your favorite buildings.
Using JAVA For this assignment, you will analyze code that uses a file input stream and...
Using JAVA For this assignment, you will analyze code that uses a file input stream and a file output stream. Read through the linked Java™ code. In a Microsoft® Word document, answer the following questions: Could this program be run as is? If not, what is it lacking? Does this program modify the contents of an input stream? In what way? What are the results of running this code? ********************************************** CODE TO ANALYZE  ******************************************************** /********************************************************************** *   Program:   Datasort *   Purpose:   ...
You are to write a C program that will read from a file, one or more...
You are to write a C program that will read from a file, one or more sets of x,y coordinates. Each set of coordinates is part of a Cartesian system. A Cartesian coordinate system is a system that specifies each point uniquely in a plane by a pair of numerical coordinates. Your program will determine which quadrant each set belong. - Quadrants are often numbered 1st - 4th and denoted by Roman numerals: I(+,+), II (−,+), III (−,−), and IV...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT