Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> //For standard input output #include <cstdio> // For sprintf

ID: 3584954 • Letter: #

Question

#include <iostream> //For standard input output
#include <cstdio> // For sprintf which nstead of being printed,
#include <cstdlib> // FOr using sustem calls

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; // 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 t recover
}

int close(int fd);

char stringbuffer[1024]; // 1024-byte array of chars

// open a couple of files
fd1 = open("file1", 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); }

ssize_t write(int fd, const void *buf, size_t count);

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);

// 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); }

Explanation / Answer

1. Please include necessary header files.

2. Close the descriptor

Please follwo the below program as an example:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
int fd,ret;
char *buf;   

if (argc < 2) {
printf("Usage : %s pathname ", argv[0]);
exit(1);
}

if ((fd = open(argv[1], O_RDONLY)) < 0) {
perror("Problem opening the file");
exit(1);
}

/*reading*/
while ((retval = read(fd, buf, 1)) > 0)
printf("%c", *buf);   
if (ret < 0) {
perror(" Read failure");
exit(1);
}
printf(" Successfully read bytes from the file %s ", argv[1]);

close(fd);
}