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...
Read a text file into arrays and output - Java Program ------------------------------------------------------------------------------ I need to have...
Read a text file into arrays and output - Java Program ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards to the other...
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.
In Java language. Write a brief program that writes your name to a file in text...
In Java language. 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 java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT