Question

In: Computer Science

here it is running nonstop. so here one thing should be wrong. your task is solving...

here it is running nonstop. so here one thing should be wrong. your task is solving that problem 

#include <stdio.h>
#include<unistd.h>

int main(int argc, char *argv[]){  
        FILE *fd;
        
        unsigned char ch;  //what will happen if you uncomment this line and comment the                //next line
        //char ch;

        int fileSize=-1;

        fd = fopen(argv[1], "r"); 

        do{
                ch=getc(fd);  //0xFF
                fileSize++;
                printf("fileSize=%d\n", fileSize);
                printf("Char read is ch=%c, in hex ch=%#hhx EOF is %#x\n\n", ch, ch, EOF);
                sleep(1);
        } while( ch != EOF);  //ch =0x FF,  EOF=0x FFFF FFFF
        
        printf(" \nout of do while loop now.\n\n");
        printf("ch=%d EOF=%#x\n", ch, EOF);
        printf("size of char =%ld size of EOF=%ld\n", sizeof(char), sizeof(EOF));
        
        printf("Size of %s is %d\n", argv[1], fileSize);
}


//suggeson: levae all the printf statment in place
//run the code to observe the output
//then use gdb to find out the "bug"

this code running nonstop, correct this code

Solutions

Expert Solution

Thre reason it is running infinitely is because you are reading the file using unsigned char ch . So what happens is that at the end of while loop, it checks for EOF but EOF is a signed value. Now you are comparing signed value with unsigned value. So the condition will always result in true and while lioop will loop continuously. One other reason can be the implementation of compiler. Sometimes compilers have definition of EOF as -1 . Now again the problem of signed and unsigned comparing will occur and loop will always result in true condition. To avoid this ,use char ch instead of unsigned char ch. When you will use char ch, it will not loop infinitely.

Below is the the code of same program in Windows. In windows sleep() method is Written as Sleep() [capital S] . And we need to include <windows.h> other than this the code is completely same. If you are not using windows then dont include these differences. Just comment the line unsigned char ch and uncomment char ch. now run the program and it will work fine.

Windows Environment program with output:-->>

#include <stdio.h>
#include<windows.h>
#include<unistd.h>

int main(int argc, char *argv[]){
        FILE *fd;

        //unsigned char ch;  //what will happen if you uncomment this line and comment the                //next line
        char ch;

        int fileSize=-1;

        fd = fopen(argv[1], "r");

        do{
                ch=getc(fd);  //0xFF
                fileSize++;
                printf("fileSize=%d\n", fileSize);
                printf("Char read is ch=%c, in hex ch=%#hhx EOF is %#x\n\n", ch, ch, EOF);
                Sleep(1);
        } while( ch != EOF);  //ch =0x FF,  EOF=0x FFFF FFFF

        printf(" \nout of do while loop now.\n\n");
        printf("ch=%d EOF=%#x\n", ch, EOF);
        printf("size of char =%ld size of EOF=%ld\n", sizeof(char), sizeof(EOF));

        printf("Size of %s is %d\n", argv[1], fileSize);
}

OUTPUT:

Look at console there is no infinite loop.


Related Solutions

Sorry, I originally pasted the wrong thing. Here is the problem: You are given the following...
Sorry, I originally pasted the wrong thing. Here is the problem: You are given the following information for Bowie Pizza Co.: Sales = $69,000; Costs = $31,700; Addition to retained earnings = $6,200; Dividends paid = $2,130; Interest expense = $4,900; Tax rate = 25 percent. Calculate the depreciation expense.
Oh No! One of the tRNA genes in E.coli was mutated so that the wrong amino...
Oh No! One of the tRNA genes in E.coli was mutated so that the wrong amino acid was charged onto it. Now, the amino acid that should be on the tRNA containing the anticodon 5'-CCA-3' is replaced with an Asp. DNA SEQUENCE: 5'-ATG TAG TTA TCC AAC CCT CCA AAA CAT GCA CCT CCT TAC TCA AGC AGG CAT ACG-3' a. Write out the protein sequence (using the DNA sequence from the problem above) that would be made in this...
Answer this fully please, type your answer, someone already answered it here but it was wrong,...
Answer this fully please, type your answer, someone already answered it here but it was wrong, only answer (((Quick ratio: ))))))and its not 1.2, 1.2 if you get any of these numbers dont reply to my question please PepsiCo, Inc. (PEP), the parent company of Frito-Lay snack foods and Pepsi beverages, had the following current assets and current liabilities at the end of two recent years: Current Year (in millions) Previous Year (in millions) Cash and cash equivalents $9,158 $9,096...
What is one thing about copyright law that people should be more aware of as they...
What is one thing about copyright law that people should be more aware of as they proceed into a career and why it's important.
1. Discuss one thing that you feel should remain the same with the new health care...
1. Discuss one thing that you feel should remain the same with the new health care legislation that is expected from the current Administration, OR, discuss one change that you feel is greatly needed. Be sure to provide a solid rationale for what you believe should remain the same or what aspect of the legislation should change. 2. Discuss the concept of Managed Care Organizations (MCOs). What is the goal of an MCO? Briefly discuss one strategy that is used...
1.) PERIODIC 2.) PERPETUAL So your task to start this discussion is to define each AND...
1.) PERIODIC 2.) PERPETUAL So your task to start this discussion is to define each AND which one do you think Walmart/Target/etc. uses and why?
Your task is to write an article describing the differences you have found or noticed so...
Your task is to write an article describing the differences you have found or noticed so far between C++ and Java. First you should go through the W3schools C++ tutorials as described in the week’s description, taking some notes about the differences between C++ and Java as you do so. Then, you should write your document. Your document does not need to be long, but it should be well-written. Imagine that you are writing this for Java programmers who want...
This is Physics 2, College Level: Someone answered wrong so take your time, do not rush...
This is Physics 2, College Level: Someone answered wrong so take your time, do not rush to answer these questions. Explain how you got the answers. A 1.00 F capacitor is charged to 6.00 V. The capacitor and an open switch is connected to a coil of wire that consists of 500 windings, which has a resistance of 1.20 Ω. This coil has a diameter of 5.00 cm and a length of 16.0 cm. A slightly smaller coil is placed...
Your final journal of the semester is here. This one is a bit more relaxed. One...
Your final journal of the semester is here. This one is a bit more relaxed. One of the hopeful and apparent themes throughout the course is how all of the systems are interrelated in one way or another. No one system functions in isolation or independent of any other system. With that said. Select any system within the body (even from A&P I) and link it to at least 2 other systems. Explain the homeostatic, endocrine, negative feedback (as appropriate)...
Your task is this: conduct one of the two possible tests on the data in slide...
Your task is this: conduct one of the two possible tests on the data in slide 14 (regression or correlation). Refer to the previous slides in doing the exercise. Be sure to provide a write up (narrative), which explains both the statistical significance (slide 8) and the practical significance (slide 11). A sentence for each kind of significance is sufficient. Perform the appropriate test and provide a narrative write up for the Depression and Anxiety analysis. Remember, state the statistical...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT