Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

#include #include #include #include #include #include

using namespace std; char script[600];

int main( int argc, char** argv ) { cout << "You have entered " << argc-1 << " arguments:"; for (int j = 1; j < argc; ++j) { sprintf(script, "%s %s", "cat ", argv[j]); // content stored as a C string in the buffer pointed by str. printf(" ***************Start of File******************* "); system(script); // system call to execute the command printf(" *******************End Of File******************* "); return 0; } int open(const char *pathname, int flags); int fd,fd1,fd2;//declaring all three here fd = open("path_to_a_file", O_RDWR | O_CREAT, 0755); if(fd == -1) { perror("opening file");

} int close(int fd); char stringbuffer[1024];

fd1 = open("file1", O_RDONLY); ssize_t howmany; howmany = read(fd1, stringbuffer, 1024); if(howmany==-1) { perror("reading file 1"); exit(1); } howmany = read(fd2, &data, sizeof(some_struct)); if(howmany==-1) { perror("reading file 2"); exit(2); } ssize_t write(int fd, const void *buf, size_t count); char mystring[] = "Some text to write."; char * pointer = (char *) mystring; fd1 = open("file1", O_WRONLY); fd2 = open("file2", O_WRONLY); // write the character array to file 1 howmany = write(fd1, mystring, sizeof(mystring)); if(howmany==-1) { perror("writing to file 1"); exit(1); } // sizeof gives the wrong answer for pointers, use strlen howmany = write(fd2, pointer, strlen(pointer)); if(howmany==-1) { perror("writing to file 2"); exit(2); }

Im getting these errors please help

prog2.cpp: In function 'int main(int, char**)': prog2.cpp:27:53: error: too many arguments to function 'int open(const char*, int)' fd = open("path_to_a_file", O_RDWR | O_CREAT, 0755); ^ prog2.cpp:25:7: note: declared here int open(const char *pathname, int flags); ^ prog2.cpp:39:24: error: 'data' was not declared in this scope howmany = read(fd2, &data, sizeof(some_struct)); ^ prog2.cpp:39:37: error: 'some_struct' was not declared in this scope howmany = read(fd2, &data, sizeof(some_struct));

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Genomes And Databases On The Internet A Practical Guide To Functions And Applications

Authors: Paul Rangel

1st Edition

189848631X, 978-1898486312

More Books

Students also viewed these Databases questions

Question

Explain the strength of acid and alkali solutions with examples

Answered: 1 week ago

Question

Introduce and define metals and nonmetals and explain with examples

Answered: 1 week ago