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

(1) Describe a scenario that justifies the requirement to check the return value

ID: 3874551 • Letter: #

Question

(1) Describe a scenario that justifies the requirement to check the return value from fclose().
(2) Describe the four parameters to fread().
(3) Describe the second parameter to fopen().
(4) If a fopen() call returns the hexadecimal value 0x123456, what does that indicate?
(5) What is the name of the struct that is used to determine the creation date of c:windows?
(6) Write lines of code that would determine and display the creation date of c:windows?
(7) What is the minimum value of argc?
(8) If your program has one argument on the command line, what is the value of argc? Conceptually, describe what the values of argv[0], argv[1], and argv[2] are.
(9) When would you use two backslashes in a double-quoted string in your source code? Conversely, when would you use one backslash?
(10) Is it more appropriate to use one or two backslashes when specifying an argument on the command line?

Explanation / Answer

(1) Describe a scenario that justifies the requirement to check the return value from fclose().
If a file is opened in first program, and second program tries to write and close the stream, then in that case, the file may not be writable. Hence, fclose will return EOF. In this case, checking this helps us in determining whether the file is written successfully or not.

(2) Describe the four parameters to fread().
size_t fread(void *ptr, size_t blocksize, size_t nmemb, FILE *stream)
ptr This is the pointer to a block of memory with a minimum size of blocksize*nmemb bytes.
blocksize This is the size in bytes of each element to be read.
nmemb This is the number of elements, each one with a size of blocksize bytes.
stream This is the pointer to a FILE object that specifies an input stream.

(3) Describe the second parameter to fopen().
FILE *fopen(const char *filename, const char *mode)
The second parameter determine in which the file is opened. Valid modes are: read, write, append, read+write etc.

(4) If a fopen() call returns the hexadecimal value 0x123456, what does that indicate?
fopen() returns the address of the memory location, from where the file has started.

(5) What is the name of the struct that is used to determine the creation date of c:windows?
struct stat;
in stat, st_ctime is the member which stores the creation date of the file.

(6) Write lines of code that would determine and display the creation date of c:windows?
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>

int main(int argc, char **argv)
{
struct stat t_stat;
stat("c:\windows", &t_stat);
struct tm * timeinfo = localtime(&t_stat.st_ctime); // or gmtime() depending on what you want
printf("File time and date: %s", asctime(timeinfo));

return 0;
}

(7) What is the minimum value of argc?
1, atleast one argumnt is required, i.e. name of executable. program can be run using the name of executable..

(8) If your program has one argument on the command line, what is the value of argc? Conceptually, describe what the values of argv[0], argv[1], and argv[2] are.
The name of the executable is at index 0 at argv. the argument is at index 1 of argv. As there is just one argument, hence argv[2] will be null.

(9) When would you use two backslashes in a double-quoted string in your source code? Conversely, when would you use one backslash?
The backslash is used to print the specific character such as new line, carriage return, tab etc. When we need to keep a slash in the string, then it is prepended with a backslash to indicate that it need to print a backslash. If we need to print a newline, then we use " ", single backslash like this.

(10) Is it more appropriate to use one or two backslashes when specifying an argument on the command line?
Single slash, as if we represent "\" in a strign in prorgam, only a single slash gets saved in memory.. so on command line, we dont need to escape the characters.. whatever chaarcter we specify, they will go in memory as it is.