Question

In: Computer Science

****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED THIS MEANS THAT...

****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED THIS MEANS THAT YOU CANT USE fopen, fclose , etc

*****You are ONLY ALLOWED TO USE SYSTEM CALL I/O such as read, write, open and close (System I/O functions)

Write a C program using system call I/O to

a) open an existing text file passed to your program as a command-line argument, then

b) display the content of the file,

c) ask the user what information he/she wants to append

d) receive the info from the user via keyboard

e) append the info received in d) to the end of the file

f) display the updated content of the file.

Solutions

Expert Solution

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main (int argc, char *argv[])
{
    int fd1,n;
    char buf[1024],buf1[100];
    
    // To open the file rd,wr,append mode
    //fd1 = open("a.txt", O_RDWR |  O_APPEND);
    fd1 = open(argv[1], O_RDWR |  O_APPEND);
    if (fd1 == -1) {
        perror("File cannot be opened");
        return EXIT_FAILURE;
    }
    
    // To display "Content of File::" on screen
    char *s="\nContent of File::\n";
    write(1,s,strlen(s));  // write(file descriptor,string address,no_of_bytes to be wriiten)
                           //File descriptor 1 is standard output.
    
    
    //To read content of given file and display
    n=read(fd1, buf, sizeof(buf));   //no_bytes_read=read(file descriptor,buffer,size);
    write(1, buf, n);               //write(file descriptor,buffer,no_of_bytes);
    //close(fd1);                     // close
    
    
    // To display the message on screen
    char *s1="\nEnter the string to append to the file\n";
    write(1,s1,strlen(s1));
    
    // To read string from user
    read(0,buf1,100); //File descriptor 0 is standard input(key board).
    
    
    //open the file in append mode to write the string at the end of file
    //fd1 = open("a.txt",O_WRONLY | O_APPEND);
    write(fd1, buf1, strlen(buf1)); 
    close(fd1);   //close file descriptor
    
    //To display message
    char *s3="\nContent of File after appending::\n";
    write(1,s3,strlen(s3));  // write(file descriptor,string address,no_of_bytes to be wriiten)
                           //File descriptor 1 is standard output.
    
    
    //To read content of given file and display
    //fd1 = open("a.txt",O_RDONLY);
    fd1 = open(argv[1],O_RDONLY);
    n=read(fd1, buf, sizeof(buf));   //no_bytes_read=read(file descriptor,buffer,size);
    write(1, buf, n);               //write(file descriptor,buffer,no_of_bytes);
    close(fd1);                     // close
   
    return 0;
}

int open (char* fileName, int mode [, int permissions])

   O_RDONLY    Open for reading only.
   O_WRONLY    Open for writing only.
   O_RDWR      Open for reading and writing.

O_APPEND: Position the file pointer at the end of the file before each write ().

ssize_t read (int fd, void* buf, size_t count)

ssize_t write (int fd, void* buf, size_t count)

int close (int fd)


Related Solutions

****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED THIS MEANS THAT...
****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED THIS MEANS THAT YOU CANT USE fopen, fclose , etc *******You are ONLY ALLOWED TO USE SYSTEM CALL I/O such as read, write, open and close (System I/O functions) Write a C program using system call I/O to determine how many lines there are in a text file.
****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED Write a C...
****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED Write a C program using system call I/O to determine how many lines there are in a text file.
Write a C program using system call I/O to ****NOTE YOU MUST USE SYSTEM CALL I/O,...
Write a C program using system call I/O to ****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED a) open an existing text file passed to your program as a command-line argument, then b) display the content of the file, c) ask the user what information he/she wants to append d) receive the info from the user via keyboard e) append the info received in d) to the end of the file f) display the updated...
Write a C program using system call I/O to a) open an existing text file passed...
Write a C program using system call I/O to a) open an existing text file passed to your program as a command line argument, then b) display the content of the file, c) ask the user what information he/she wants to append d) receive the info from the user via keyboard e) append the info received in d) to the end of the file f) display the updated content of the file
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: For this exercise you should be able to write a logical expression (i.e., with logical operators) which checks if some integer x consists of exactly 5 digits. Ex: 30498 and -14004 are 5-digit numbers, while 1098, -1 and 34 are not. Complete the intQ2(intQ2_input) function that takes an input integer...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: A positive integer number is said to be a perfect number if its positive factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6=1+2+3. Complete the int Q6(intQ6_input, int perfect[])function that determines all perfect numbers smaller than or equal...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: (Pythagorean Triples) A right triangle can have sides that are all integers. The set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the relationship that the sum of the squares of two of the sides is equal...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: Complete the int Q7a(intQ7_input) function takes only a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: Complete the int Q7a(intQ7_input) function takes a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use arrays...
You are not allowed to import anything • You are not allowed to use slicing •...
You are not allowed to import anything • You are not allowed to use slicing • You are not allowed to use sets or dictionaries • You are not allowed to use any string method • You are not allowed to use anything that hasn’t been covered in class • No built-in function except range() and len() is allowed • From list methods, you are allowed to use .append(), .insert(), .remove() or del. Please do not ask on piazza whether...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT