Question: Please write in C++ shownames.c: #include #include #include int main (int argc, char *argv[]) { struct dirent *direntp; // define a pointer with type directory

Please write in C++

Please write in C++ shownames.c: #include #include #include int main (int argc,

shownames.c:

#include

#include

#include

int main (int argc, char *argv[])

{

struct dirent *direntp;

// define a pointer with type directory stream (DIR):________

// check the number of arguments (argc). It should be two. If it

// is not two, then print an error message and exit the program:

if (__________)

{

fprintf (stderr, "Usage: %s directory name ", argv[0]);

return 1;

}

// The following lines should open the directory given by

// argument argv[1] (by using opendir).

// Store it in your defined directory stream variable.

// The rest checks if the system call returned a proper value

// if not an error message is printed and the program closed

if ( ________ == NULL)

{

perror ( "Failed to open directory" );

return 1;

}

// Read all the entries in this directory and store them in

// direntp. readdir will read one entry at a time and increment

// automatically. This is why it is in a while loop.

// Then, print all the file names (using the struct from readdir).

while (_______ != NULL)

printf ("%s " , _______ );

// close the defined directory stream.

while ( ( closedir (_______) == -1) && ( errno == EINTR) );

return 0;

}

Task 1: Printing File Names using Dirent Library: (50points) Here we want to access a Linux directory using C. To do so we use the structs and functions made available in the dirent library. Below we see the function prototypes for the functions opendir, readdir, and closedir which have been implemented in the dirent library. DIR is simple type that is available from the library. SYNOPSIS #include DIR *opendir (const char *dirame); struct dirent *readdir (DIR * dir ); int closedir (DIR * dir ); Dirent structure is defined as follows: struct dirent { ino_t d_ino; /* file serial number */ off_t d_off; /* offset to the next dirent */ unsigned short d_reclen; /* length of this record */ unsigned char d_type; /* type of file */ char d_name [256]; /* filename * The following program displays the filenames contained in a directory whose pathname is passed as a command-line argument. Type in and complete the following program on the computer. Save it as shownames.c. Compile the program as shownames.out and execute it by giving it a directory as a command line argument. For example type the following into the terminal: J shownames.out -/CSC4421. If you typed that then the program below will receive values of: argc==2, argv[0]=="/shownames.out", argv[1]=="~/CSC4421". shownames.c

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!