Purpose: The purpose of this assignment is to practice reading and writing from
ID: 3585040 • Letter: P
Question
Purpose:
The purpose of this assignment is to practice reading and writing from files using the UNIX system calls. You will be writing a basic implementation of the cat command using C++.
Description If you recall, the cat command takes a list of files as command line arguments, and then opens each file, dumping its contents to standard output, in the order the filenames were passed in. You will be implementing this behavior.
Requirements • Your program must handle any number of files, which will have their filenames passed as command line parameters.
• No matter how long each file is, your program must display all of the data inside. • If an input file is not a text file, make sure that all of the data is displayed. This means that you will not be able to use cout on its own to output the data.
• You must use the UNIX system calls we spoke about in class to implement the reading portion. You can use them for the writing portion as well, but this is not required. This means that you cannot use the C++ file stream classes for the file input (open, read, close).
• Make sure to clean up after you are done with each file, calling close on its file descriptor.
Opening File
int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode);
int fd; // file descriptor, or error code
fd = open("path_to_a_file", O_RDWR | O_CREAT, 0755);
if(fd == -1) {
perror("opening file"); // print human readable error // deal with the error, maybe exit if you can’t recover }
Closing Files
int close(int fd);
Reading from Files
ssize_t read(int fd, void *buf, size_t count);
char stringbuffer[1024]; // 1024-byte array of chars
struct some_struct data; // structured data
// open a couple of files
int fd1 = open("file1", O_RDONLY);
int fd2 = open("file2", O_RDONLY);
ssize_t howmany; // how many bytes read?
// reading from a file into a string buffer
howmany = read(fd1, stringbuffer, 1024);
if(howmany==-1) { perror("reading file 1"); exit(1); }
// read data from a file directly into a data structure
howmany = read(fd2, &data, sizeof(some_struct));
if(howmany==-1) { perror("reading file 2"); exit(2); }
Writing to Files
ssize_t write(int fd, const void *buf, size_t count);
// string literal stored as character array
char mystring[] = "Some text to write.";
// pointer to start of character array from above
char * pointer = (char *) mystring;
// open a couple of files
int fd1 = open("file1", O_WRONLY);
int fd2 = open("file2", O_WRONLY);
ssize_t howmany;
// how many bytes read? // write the character array to file 1
howmany = write(fd1, mystring, sizeof(mystring));
if(howmany==-1) { perror("writing to file 1"); exit(1); }
howmany = write(fd2, pointer, strlen(pointer));
if(howmany==-1) { perror("writing to file 2"); exit(2); }
Explanation / Answer
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
using namespace std;
int main(int argc , char **argv){
char *buf;
int fd;
int bytes_read;
int size;
for (int i = 1; i<argc; i++){
if((fd = open(argv[i], O_RDONLY)) < 0)
{
cout << "Couldn't open the file ";
}
else {
lseek(fd,0,2);
size = lseek(fd, 0, SEEK_END);
buf = (char *)malloc(sizeof(char)*size);
lseek(fd,0,0);
while (bytes_read = read(fd,buf,size) > 0){
write(STDOUT_FILENO,buf,size);
}
close(fd);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.