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

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

ID: 3585599 • Letter: #

Question

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

//using namespace std;
char script[600];

int main( int argc, char** argv )
{
   int j;
   char data[600];
   //cout << "You have entered " << argc-1 << " arguments:";
   for (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, some_struct;
   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);
   }
   return 0;
}

Getting "bad file descriptor" please help fix

Explanation / Answer

In the program above, there are 4 reasons that you can get the "bad file descripter".

Please find below the modified program below. This is sucessfulled complied with in C using CodeBlocks:

#include <fcntl.h>

#include <unistd.h>

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <string.h>

#include <stdlib.h>

//using namespace std;

char script[600];

int main( int argc, char** argv )

{

int j;

char data[600];

//cout << "You have entered " << argc-1 << " arguments:";

for (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("read1.txt", O_RDONLY); // Line #32: As File is opened using Readonly mode so it should exists

ssize_t howmany, some_struct;

howmany = read(fd1, stringbuffer, 1024);

if(howmany==-1) {

perror("reading file 1");

exit(1);

}

fd2 = open("read1.txt", O_RDONLY); // Line #38: Required for next line. File needs to be open to read it.

howmany = read(fd2, &data, sizeof(some_struct)); // Line #39/40: File fd2 is not defined/Opened

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("write1", O_CREAT | O_WRONLY, 0777); // Line #49: As you are opening a file which is not there its giving error. SO you need to add a flag O_CREAT also. apart from this once the file will be created it should have some permission which is going to be third parameter. so set that to 0777 or what you feel better as per security concerns.

fd2 = open("write2", O_CREAT | O_WRONLY, 0777); // Line #49: The same issue here as explained above. Set O_CREAT Flag as if file doesn't exists it will add the same and do not forget to set permission. otherwise with default permission you will not be able to open/write the file again.

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

}

return 0;

}