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

Anwer the following C program questions: (11) Why do you have to call fclose()?

ID: 3579525 • Letter: A

Question

Anwer the following C program questions:
(11) Why do you have to call fclose()?
(12) Why do you have to call FindClose()?
(13) If you try to read 1000 bytes from a file using fread() but the file only contains 100 bytes, describe what will happen with the fread() call.
(14) If you use fread to read 100 bytes at a time from a file that contains 1000 bytes, how many calls to fread would it take before you could tell that you were at the end of the file?
(15) What is the difference between a text file and a binary file?
(16) What function would you likely use to read from a binary file? What function would you likely use to read from a text file?
(17) If you use fgets() to read a line in from a text file and the input does not have a ' ' in it, what do you know about the input that the user has entered?
(18) If you have 10 lines of text in a text file, how many '' characters would you likely find contained within the contents of the file?
(19) Why is the answer not 10? (Hint: the answer isn't 9 or 11, either.)
(20) Write a line of code that uses calloc() to allocate a block that can contain an array of 10 struct Parts. Assume that you have a variable called pBlock that is declared appropriately.
(21) Declare the pBlock variable from the previous question.
(22) Describe the criteria for how to split up a 100,000 line program into multiple files?
(23) Describe how the bitwise-AND operator can be used to determine if a file has a particular attribute.

Explanation / Answer

(11) Why do you have to call fclose()?

Fclose() flushes the given stream buffers and then disassociates the stream from the pointer to FILE. A file must be closed as soon as all operations on it have been completed. This would close the file associated with the file pointer.

Syntax : int fclose(FILE *stream)

FILE *k1,*k2;

K1 = fopen(“sample_input”,”w”);

K2 = fopen(“Sample_output”,”r”);

……

……

Fclose(k1);

Fclose(k2);

(12) Why do you have to call FindClose()?

            FindClose() is a function used to close the File Search operation, after being opened a connection to search the file using FindOpen(). Once finding the file in particular directory, the operation will be closed by defining findclose().

            Syntax FindClose(string filepath, FILE filename)

            Eg. FindClose(“c:\*.*”, “hello.txt”)

(15) What is the difference between a text file and a binary file?

A text file is human readable and understandable data. It contains the characters which can be easily encoded by any encoders. It even includes things like HTML or XML, which are often human-readable by a very generous definition. It can still open them in a text editor.

Binary files are machine understandable files which cannot be understand by human. It contains collections of bytes that ranges from 0 to 255. It cant be encoded by any encoders.

(23) Describe how the bitwise-AND operator can be used to determine if a file has a particular attribute.

            The file attribute for checking read only is a bitwise AND. This means that it will return the value ReadOnly if the appropriate bit is set in attributes, otherwise it will return 0.

A bitwise AND takes two binary representations of equal length and performs the logical AND operation on each pair of corresponding bits. The result in each position is 1 if the first bit is 1 and the second bit is 1; otherwise, the result is 0.

(16) What function would you likely use to read from a binary file? What function would you likely use to read from a text file?

The function used to read text file is

char *fgets(char *ptr, int size, FILE *fptr); (OR)

int fscanf(FILE *fptr, char *string_seek ...)

The function used to read Binary file is:

size_t fread(void *buffer, size_t size, size_t num,FILE *fp);

(20) Write a line of code that uses calloc() to allocate a block that can contain an array of 10 struct Parts. Assume that you have a variable called pBlock that is declared appropriately

            It allocates memory similar to malloc() function and in addition it initializes the allocated memory with 0’s. It allocates memory equal to nitems*size and returns starting address after initializing the memory.

            Syntax of calloc is :   void *calloc(size_t nitems, size_t size)

            pBlock = (pBlock)* calloc(10,sizeof(pBlock))

(21) Declare the pBlock variable from the previous question.

                        Struct pBlock {

                        };

            Struct pBlock *pbl;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote