Hi. I have a bit of a problem with this code:

#include <stdio.h>      
#include <conio.h>
#include <stdlib.h>

main () {
    FILE *data;
    char let;
    int prior, ctr;
    data = fopen("huffman_table.txt", "r");
    while (fscanf(data, "%c%d\n",&let, &prior)!='\0')  {
       printf("\nLetter: %c Priority: %d", let, prior);
    }
    fclose(data);
  
 getche();    
}

The huffman_table.txt contains the content:
B1
H1
J4
G5
F6
C7
I8
A9
D9
E10

The code works well until the last line. My program ended up having endless loops with "E10". I double-checked the txt file and it is completely fine. Any help with the code is much appreciated! :)

*btw, i am required to use fscanf to read the content.

jephthah commented: code tags. summary of problem. requrements to use fscanf. first post SUCCESS :) +7

Here's what manpages has to say about return values for fcsanf()

RETURN VALUE
These functions return the number of input items successfully matched
and assigned, which can be fewer than provided for, or even zero in the
event of an early matching failure.

The value EOF is returned if the end of input is reached before either
the first successful conversion or a matching failure occurs. EOF is
also returned if a read error occurs, in which case the error indicator
for the stream (see ferror(3)) is set, and errno is set indicate the
error.

it's not correct to test the return value of fscanf as a NULL character. fscanf returns an int. specifically:

Upon successful completion, these functions return the number of successfully matched and assigned input items; this number can be 0 in the event of an early matching failure. If the input ends before the first matching failure or conversion, EOF is returned. If a read error occurs the error indicator for the stream is set, EOF is returned, and errno is set to indicate the error.

--opengroup.org

and EOF is a macro that is represented as a negative value.

so change your line to

while (fscanf(data, "%c%d\n",&let, &prior) > 0)

this will process as long as characters are read.

Thank you for the info and help! :)

#include <stdio.h>      
#include <conio.h>
#include <stdlib.h>
#define EOF -1

main () {
    FILE *data;
    char let;
    int prior, ctr;
    if((data = fopen("huffman_table.txt", "r+"))!='\0')
    {
         while (fscanf(data, "%c%d\n",&let, &prior)!=EOF)
        {
            printf("\nLetter: %c Priority: %d", let, prior);
        }
   }
    fclose(data);

 getche();    
}

The huffman_table.txt contains the content:

B1
H1
J4
G5
F6
C7
I8
A9
D9
E10

The code works well until the last line. My program ended up having endless loops with "E10". I double-checked the txt file and it is completely fine. Any help with the code is much appreciated! :)

btw, i am required to use fscanf to read the content.

who are you?

where did you come from?

why don't you have your own thread?

what the hell is that deprecated conio.h doing in your code.

check the forum rules before you post. the big fat sticky threads at the top of the C forum.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.