Question

In: Computer Science

I need a brief text file to explain why the program “filesize1v.c” gets stuck in the...

I need a brief text file to explain why the program “filesize1v.c” gets stuck in the “do/while” loop (i.e., explain why “ch != EOF” is always true) ??

example.txt :

hello
ÿhello everyone.

_________________

//filesize1v.c
#include 
#include 

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"

Solutions

Expert Solution

//filesize1v.c
#include 
#include 


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"

You are getting this error and stuck on the do/while loop becuase in the code we are using the unsigned char and the EOF is a signed int quantity , so these value can never be equal in any case, so the code is goin in the infinite loop , you need to declare the ch as the int so that these values can be compared, so unsigned char ch , can only contain the number or the magnitude without any sign in front and the EOF is defined as -1 in the stdio.h and the other programming langugaes as well , so , the values when compared will always give True as a result because they are not eqaul , so declare the ch as signed int.


Related Solutions

I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
In JAVA Write a brief program that writes your name to a file in text format...
In JAVA Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
Part I The input to the program will be a text file containing the information for...
Part I The input to the program will be a text file containing the information for a tolerance table. An example follows using the values from the first lecture on tolerance analysis. These values will be stored in a text file. The data is comma delimited, which means that each data field is separated by a comma. If the first word is ‘PART’ the following values are the nominal size, +/- impact, tolerance, and fixed/variable. If the first word is...
I just finished this program where the program reads a text file of full names ,...
I just finished this program where the program reads a text file of full names , first and last name, and a zip code. It reads the items then stores the first name and last name and zipcodes as an objects and prints the items.   However, when i use my text file i get this error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 " I notice this issue only happens when the names are...
Write a program that computes and prints the average of numbers in a text file. I...
Write a program that computes and prints the average of numbers in a text file. I created a text file integers.txt that has the numbers 5,4,3,2,1. I need to define the average function Define the main function which will include the following things 1. prompt user for input of text file name 2. open and read input file, can be done before or inside high order functions 3. use two high order functions 4.calculate and display averages and original ist...
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
How do I do this: Write a program that can read a text file of numbers...
How do I do this: Write a program that can read a text file of numbers and calculate the mean and standard deviation of those numbers. Print the result in another text file. Put the result on the computer screen. EACH LINE OF THE PROGRAM MUST BE COMMENTED!
I need original java code that completes this program and gets it to print out results...
I need original java code that completes this program and gets it to print out results just like in the example. Also please upload answer in a word document format only. Implement both linear search and binary search, and see which one performs better given an array 1,000 randomly generated whole numbers (between 0-999), a number picked to search that array at random, and conducting these tests 20 times. Each time the search is conducted the number of checks (IE...
Please solve all the following questions. I need the text file and screenshots of encryption and...
Please solve all the following questions. I need the text file and screenshots of encryption and decryption. Exchange of encrypted data. a. Encrypt a file (e.g., a text file) with an algorithm and a key length of your choice. b. Exchange the file and the necessary credentials for decryption (i.e., algorithm, key) with your neighbor. c. Decrypt the secret of your neighbor.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT