Please find the program to read and write to output file. Also i have added necessary comments in it.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
enum { BUFFER_SIZE = 16 };
int main(int argc, char *argv[])
{
int n;
unsigned char read_char[BUFFER_SIZE]={'\0'};
int fptr, fptw;
/* check for format */
if(argc != 3)
{
printf("Usage: %s <input_file>
<output_file>\n", argv[0]);
return -1;
}
/* open the file */
if ((fptr = open(argv[1], O_RDONLY )) < 0)
{
printf("Error: %s %s\n",argv[1],
strerror(errno));
return -1;
}
/* new file wil be created if output file is not
found,
* if output file found, content will be appended
to outfile. */
if ((fptw = open(argv[2], O_CREAT |O_APPEND|O_WRONLY)
) < 0)
{
printf("Error: %s %s\n",argv[2],
strerror(errno));
return -1;
}
if(fchmod(fptw, 0) == -1)
{
printf("Error: %s %s\n",argv[1],
strerror(errno));
return -1;
}
/* change the file mode to -rw-r----- */
if(fchmod(fptw, S_IRUSR |S_IWUSR |S_IRGRP ) ==
1)
{
printf("Error: %s %s\n",argv[1],
strerror(errno));
return -1;
}
/* read the file until read returns 0 */
n= read(fptr, read_char, BUFFER_SIZE);
if (n == -1)
{
printf("Error: %s\n",
strerror(errno));
return -1;
}
/* writing to the file */
while (n != 0)
{
if(n < 16)
write(fptw, read_char, n);
else
write(fptw, read_char,
BUFFER_SIZE);
bzero(read_char, BUFFER_SIZE);
/* read the object file until
fread returns less than 1 */
n= read(fptr, read_char,
BUFFER_SIZE);
if (n == -1)
{
printf("Error:
%s\n", strerror(errno));
return -1;
}
}
/* close both the files */
close(fptr);
close(fptw);
return 0;
}
Output:
USER>./filecat exoplanets.dat
Usage: ./filecat <input_file> <output_file>
USER>
USER>cc file_cat.c -o filecat
USER>./filecat exoplanets.dat output_file
USER>ls -l output_file
-rw-r----- 1 asgu warp 100 Jun 4 10:03 output_file
USER>cat output_file
aaaaaa 10 3.4
bbbbbb 20 5.6
cccccc 340 7.86
dddd 230 4.574
eeee 5 7.8
ggggg 344 5.2
hhhhh 780 3.45
USER>cat exoplanets.dat
aaaaaa 10 3.4
bbbbbb 20 5.6
cccccc 340 7.86
dddd 230 4.574
eeee 5 7.8
ggggg 344 5.2
hhhhh 780 3.45
#following output shows file contents are appended
USER>./filecat exoplanets.dat output_file
USER>
USER>
USER>ls -l output_file
-rw-r----- 1 asgu warp 200 Jun 4 10:07 output_file
USER>
USER>cat output_file
aaaaaa 10 3.4
bbbbbb 20 5.6
cccccc 340 7.86
dddd 230 4.574
eeee 5 7.8
ggggg 344 5.2
hhhhh 780 3.45
aaaaaa 10 3.4
bbbbbb 20 5.6
cccccc 340 7.86
dddd 230 4.574
eeee 5 7.8
ggggg 344 5.2
hhhhh 780 3.45
USER>
UNIX is all about manipulating files and input/output streams fluidly, so it is important to get a strong grasp of how...
In Unix/Linux, input and output are treated as files and referenced by the operating system using file descriptors. When you open a shell session, for example, three file descriptors are in use: 0 standard input (stdin) 1 standard output (stdout) 2 standard error (stderr) By default, the command interpreter (shell) reads keyboard input from file descriptor 0 (stdin) and writes output to file descriptor 1 (stdout), which appears on the screen. As you explored in Lab 2, input/output can be...
Writing Unix Utilities in C (not C++ or C#) my-cat The program my-cat is a simple program. Generally, it reads a file as specified by the user and prints its contents. A typical usage is as follows, in which the user wants to see the contents of my-cat.c, and thus types: prompt> ./my-cat my-cat.c #include <stdio.h> ... As shown, my-cat reads the file my-cat.c and prints out its contents. The "./" before the my-cat above is a UNIX thing; it...
Using Unix processes Submit a README file that lists the files you have submitted along with a one sentence explanation. Call it Prj1README. MakeCopy.c : Write a C program that makes a new copy of an existing file using system calls for file manipulation. The names of the two files and copy block sizes are to be specified as command line arguments. Open the source file in read only mode and destination file in read/write mode. ForkCopy.c : Write a...
Using Unix processes Submit a README file that lists the files you have submitted along with a one sentence explanation. Call it Prj1README. MakeCopy.c : Write a C program that makes a new copy of an existing file using system calls for file manipulation. The names of the two files and copy block sizes are to be specified as command line arguments. Open the source file in read only mode and destination file in read/write mode. ForkCopy.c : Write a...
2.12 LAB 2.3: File I/O - CSV update This program shouldget names of input and output files from command line (NOT from user input)read in integers from a csv (comma-separated values) file into a vectorcompute the integer average of all of the valuesconvert each value in the vector to the difference between the original value and the averagewrite the new values into a csv file
C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...
In Python Please! 16.42 Lab 13B: Palindromes with Files Overview This is a demonstration of reading and writing files. Objectives Be able to read from an input file, perform string manipulation on each line of the file, and write to an output file. Provided input file: A single input file named myinput.txt is provided that contains a few lines of text. bob sees over the moon never odd or even statistics dr awkward Provided output file: A single output file...
Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...
How do I complete this problem? * CENGAGE MINDIAP > Terminal Opening Files and Performing FileInput in C++ 0 Opening Files and Performing File Input Flowers.cpp flowers.dat 1 // Flowers.cpp - This program reads names of flowers and whether they are grown in shade or sun from an input 2 // file and prints the information to the user's screen. 3.// Tnput: flowers.dat. 4 // Output: Names of flowers and the words sun or shade. Summary In this lab, you...
C ++ Implement cat command The purpose of this assignment is to provide practice using the system calls we discussed for working with files on a UNIX system. You will be writing a basic implementation of the cat command using C++. Description As you should recall, the cat command takes a list of files as command line arguments. It then opens each file in turn, writing each file’s entire contents to standard output in the order they were supplied. You...