Question

In: Computer Science

Please just submit text answers for questions a) b) and c) a) Use the “ls -lt...

Please just submit text answers for questions a) b) and c)

a) Use the “ls -lt filesize2.c” command to see what the size is reported by OS for this file.

In the file “filesize2.c”, the following code segment was originally commented out:

Uncomment this block of code.

/* fseek(fd, 10L, SEEK_SET);

putc(-1, fd);

rewind(fd); */ //try uncomment this block, to see what it does.

b) Think about what this code segment does.

c) Then compile and run the code with the code segment mentioned in a) uncommented. More specifically, run this program on the text file “filesize2.c” itself by typing (assuming the executable is named “filesize2”); >./filesize2 filesize2.c What is the size of “filesize2.c” that the program reports?

For a), simply write down the size of the file.

For b), simply write down what you think.

For c), simply write down the reported size by the program. Additionally, explain why the sizes reported in a) and c) are different.

//filesize2.c

#include <stdio.h>

int main(int argc, char *argv[]){  
        FILE *fd;       
        char ch;
        int fileSize=-1;
        fd = fopen(argv[1], "r+");      
         /*fseek(fd, 10L, SEEK_SET);
         putc(-1, fd);  
         rewind(fd);  */               //try uncomment this block, to see what it does.
        do{
                ch=getc(fd);  
                fileSize++;
        //      printf("fileSize=%d\n", fileSize);
        } while( ch != EOF);
        printf("Size of %s is %d\n", argv[1], fileSize);
}

Solutions

Expert Solution

NB:Every file type has a part of memory allocated which defines the type of file and its dependancies. hence the program returns the total length of char as 522 but command prompt returns 534. Hence 12 byte is allocated for filetype identifier.

Solutions:

a)

The file size of c program is (522 + documentation for filetype identifier.) = 534 as displayed after running 'ls -lt filesize2.c'

b)

The program opens the file in read mode, fseek() moves the cursor to 11th char and puts -1 equivalent char. and reset cursor to beginning When the loop runs and char is taken out one by one, at 11th position, the char comes out whose int equivalent is -1. Comparing -1 with EOF, the condition fails as EOF is equivalent to -1 in int. Hence the length is reflected as 10 rather than the actual size of the file.

c)

Size reported by command prompt: (522 + documentation for filetype identifier.) = 534
Size reported by program: 10
The code segment is part a plays trick and adds an EOF in the middle of the file which triggers the program to terminate at a position where there was no EOF initially. Hence program fails to display actual size of the file.

Hope it helps.


Related Solutions

For this assignment, please submit the answers to the following questions, as well as an Excel...
For this assignment, please submit the answers to the following questions, as well as an Excel spreadsheet which documents the work you did. Do poets die young? According to William Butler Yeats, “She is the Gaelic muse, for she gives inspiration to those she persecutes. The Gaelic poets die young, for she is restless, and will not let them remain long on earth.” One study designed to investigate this issue examined the age at death for writers from different cultures...
Instructions: Answer the following questions. Submit your answers to questions 1-5 as a Rich Text Format...
Instructions: Answer the following questions. Submit your answers to questions 1-5 as a Rich Text Format file (.rtf), Word document (.doc), or ASCII text file (.txt). For problem 6 submit an excel sheet containing your chart. 4. (40 points) Determine the number of statement executions (precise big-Oh) for each of the following sample code, as described in the lecture. Your answers should be in the form of a Big-Oh polynomial (e.g., O(3N2 + 7N + 6)). Sample #1: for (int...
Instructions: Answer the following questions. Submit your answers to questions 1-5 as a Rich Text Format...
Instructions: Answer the following questions. Submit your answers to questions 1-5 as a Rich Text Format file (.rtf), Word document (.doc), or ASCII text file (.txt). For problem 6 submit an excel sheet containing your chart. 1. (12 points) State the order of magnitude for each of the following mathematical functions. (Hint: Find the dominant term and drop its coefficient) 5n2 + 105 nlogn 5n3 – 7n + 30 (n2 / logn)+ 40000n + 1000 (5n2 + 8n + 3n)...
Please use C language to code all of the problems below. Please submit a .c file...
Please use C language to code all of the problems below. Please submit a .c file for each of the solutions, that includes the required functions, tests you wrote to check your code and a main function to run the code. Q2. Implement the quick-sort algorithm.
just use these questions as a template and plug in your answers as a bullet point...
just use these questions as a template and plug in your answers as a bullet point list of fragmented sentences, much like taking notes or writing a simple outline. You should present 3-5 bullet points for each question. However, if you feel more comfortable writing each answer in paragraph form, you certainly may do so. 1. Why did slavery become so important in the American South? 2. What were some of the conditions and treatment endured enslaved people? 3. What...
Instructions: 1. Please use only C as the language of programming. 2. Please submit the following:...
Instructions: 1. Please use only C as the language of programming. 2. Please submit the following: (1) the client and the server source files each (2) a brief Readme le that shows the usage of the program. 3. Please appropriately comment your program and name all the identifiers suitable, to enable enhanced readability of the code. Problem: Write an ftp client and an ftp server such that the client sends a request to ftp server for downloading a file. The...
Use C++ please Implement the following exercise on Visual Studios and submit the necessary (.h and...
Use C++ please Implement the following exercise on Visual Studios and submit the necessary (.h and .cpp) files in a .zip folder 1.   Create a class NumberType such that it has the following functionality: The draw function in NumberType only prints 'this is an empty function' message. Create a class NumberOne (derived from NumberType) which in the draw function prints the number using a 5(rows)x3(columns) matrix. See example at https://www.istockphoto.com/vector/led-numbers-gm805084182-130563203 * * * * * Similarly create a class NumberTwo...
Please follow the instructions and solve it by C++. Thank you! What to Submit Submit the...
Please follow the instructions and solve it by C++. Thank you! What to Submit Submit the following: 1) Your .cpp file of the solution. 2) For what n value (approximately) does your computer stop producing output? Why is this? Enter your answer in the 'Comments' field when you submit the file.   So far in our study of recursion we identified a few common recursive number sequences, such as the Fibonacci numbers. Number sequences like this are not recursive algorithms themselves...
Your Answers: Type your answers in the table and submit this worksheet. Use what you have...
Your Answers: Type your answers in the table and submit this worksheet. Use what you have learned about the time value of money to analyze each of the following decisions: Decision #1: Which set of Cash Flows is worth more now? Assume that your grandmother wants to give you generous gift. She wants you to choose which one of the following sets of cash flows you would like to receive: Option A: Receive a one-time gift of $10,000 today. Option...
A student is taking a quiz with nine questions. Each question has answers: A, B, C,...
A student is taking a quiz with nine questions. Each question has answers: A, B, C, or D. The student guesses on each question             a) how many repetitions for this problem?       b) what is the probability of success on the 1st repetition?    c) what is the probability of guessing 3 answers correctly? d) what is the probability of guessing at most 2 correct?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT