Question

In: Computer Science

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

Solutions

Expert Solution

Code Screenshot :

Executable Code:

#include <stdio.h>
//Main Program
int main(int argc, char* argv[]){
   //Opening the file
   FILE* f = fopen(argv[1], "a+");
   //Printing the existing content in the file
   printf("Existing content: \n");
   char ch;
   //Reading till the end of file
   while((ch = fgetc(f)) != EOF) printf("%c", ch);
   //Prompting the user for input
   printf("Enter info to be appended: \n");
   char content[100];
   //Reading the input
   fgets(content, 100, stdin);
   //Writing to file
   fprintf(f,"%s", content);
   fseek(f,0,SEEK_SET);
   //Printing contents of the file
   printf("File after appending: \n");
   while((ch = fgetc(f)) != EOF) printf("%c", ch);
   fclose(f);
}

Sample Output :

Please comment below if you have any queries.
Please do give a thumbs up if you liked the answer thanks :)


Related Solutions

Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
Write a C++ program to open and read a text file and count each unique token...
Write a C++ program to open and read a text file and count each unique token (word) by creating a new data type, struct, and by managing a vector of struct objects, passing the vector into and out of a function. Declare a struct TokenFreq that consists of two data members: (1) string value; and (2) int freq; Obviously, an object of this struct will be used to store a specific token and its frequency. For example, the following object...
Write a C program, called reverse, using standard I/O functions, to take a file as input...
Write a C program, called reverse, using standard I/O functions, to take a file as input then copies it to another file in reverse order. That is, the last byte becomes the first, the byte just before the last one becomes the second, etc. The program call should look like: reverse fileIn fileOut
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write a C or C++ program using the fork() system call function. You will need to...
Write a C or C++ program using the fork() system call function. You will need to create 3 processes – each process will perform a simple task. Firstly, create an integer "counter" initialized to a random value between 1 and 100. Print this number to the console. This can be done by: Including the stdio.h and stdlib.h libraries Using the rand() function to generate your randomly generated number The main thread consists of the parent process. Your job is to...
How do I write a C++ program to call a frequency table from a csv file,...
How do I write a C++ program to call a frequency table from a csv file, using vector? Data given is in a csv file. Below is part of the sample data. Student ID English Math Science 100000100 80 90 90 100000110 70 60 70 100000120 80 100 90 100000130 60 60 60 100000140 90 80 80
Write a program that computes and prints the average of numbers in a text file. I...
Write a program that computes and prints the average of numbers in a text file. I created a text file integers.txt that has the numbers 5,4,3,2,1. I need to define the average function Define the main function which will include the following things 1. prompt user for input of text file name 2. open and read input file, can be done before or inside high order functions 3. use two high order functions 4.calculate and display averages and original ist...
In c++  write a program. Give the necessary statements to open a file and to confirm that...
In c++  write a program. Give the necessary statements to open a file and to confirm that the file has been successfully opened for writing.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT