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
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
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; 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;
int fd1 = open("file1", O_WRONLY); int 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); }
getting these errors please help
error: 'fd' does not name a type fd = open("path_to_a_file", O_RDWR | O_CREAT, 0755); ^ prog2.cpp:35:1: error: expected unqualified-id before 'if' if(fd == -1) { ^ prog2.cpp:45:2: error: 'fd1' does not name a type fd1 = open("file1", O_RDONLY); ^ prog2.cpp:50:1: error: 'howmany' does not name a type howmany = read(fd1, stringbuffer, 1024); ^ prog2.cpp:51:1: error: expected unqualified-id before 'if' if(howmany==-1) { perror("reading file 1"); exit(1); } ^ prog2.cpp:54:1: error: 'howmany' does not name a type howmany = read(fd2, &data, sizeof(some_struct)); ^ prog2.cpp:55:1: error: expected unqualified-id before 'if' if(howmany==-1) { perror("reading file 2"); exit(2); } ^ prog2.cpp:65:25: error: 'O_WRONLY' was not declared in this scope int fd1 = open("file1", O_WRONLY); ^ prog2.cpp:66:25: error: 'O_WRONLY' was not declared in this scope int fd2 = open("file2", O_WRONLY); ^ prog2.cpp:69:1: error: 'howmany' does not name a type howmany = write(fd1, mystring, sizeof(mystring)); ^ prog2.cpp:70:1: error: expected unqualified-id before 'if' if(howmany==-1) { perror("writing to file 1"); exit(1); } ^ prog2.cpp:74:1: error: 'howmany' does not name a type howmany = write(fd2, pointer, strlen(pointer)); ^ prog2.cpp:75:1: error: expected unqualified-id before 'if' if(howmany==-1) { perror("writing to file 2"); exit(2); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started