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.
The one sure thing about financial projections is that they will be wrong—perhaps by only a...
The one sure thing about financial projections is that they will be wrong—perhaps by only a little, or perhaps by a lot. But managers must still make decisions. In fact, making no decision is really a type of decision—a choice to do nothing. How can you explain the uncertainties in financial projections without scaring your audience?
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...
to be done in java Your task is to finish the StockItem class so that it...
to be done in java Your task is to finish the StockItem class so that it meets the following criteria • The StockItem class will have 4 attributes: a stock number; a name; the price of the item; the total number of items currently in stock • The first three of the above characteristics will need to be set at the time a StockItem object is created, with the total number of items set to 0 at this time. The...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT