Noc
Noc

Reputation: 519

Having trouble reading Hexadecimal Input in C

I'm under the strong impression that I just don't understand how fscanf works.

I have a text file formatted like so:

0x01
0x02
0x04
0x08
0x0F
0x1F
0x2F
0x4F

And I want to read them in:

inline void read_inputs_from_ip_if(){

    if(fscanf(ipf,"%x",&input)==EOF){
        printf("Terminated.");
        exit(0);
    }

    fscanf(ipf,"%x",&input);
}

But the issue is that whenever I use a print statement on the input, it yields 0. I want to read in each line consecutively and execute some decision logic in a never-ending for loop. The issue is that it never reads in any of the inputs at all.

This is the main block:

ipf = fopen("input.txt","r");
for (; ; )
{
    read_inputs_from_ip_if();
    printf("Input: %x \n", input);
    control_action();
    write_output_to_op_if();
}

As I mentioned earlier, the input never changes from 0. I was under the impression that fscanf, due to the loop, would read a new line until it reaches the end of the file... Now I'm not so sure.

Upvotes: 1

Views: 3317

Answers (2)

LSerni
LSerni

Reputation: 57388

You simply did not specify the presence of the newline to your program.

Also, you execute "fscanf" twice, so that the program will skip one line every two.

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

int input;
FILE *ipf;

inline void read_inputs_from_ip_if(){
    if(fscanf(ipf,"%x\n",&input)==EOF){
        printf("Terminated\n");
        exit(0);
    }
}

int main() {
        ipf = fopen("fscanf-data", "r");
        for(;;)
        {
                read_inputs_from_ip_if();
                printf("Datum: 0x%02x\n", input);
        }
        fclose(ipf);
        return 0;
}

Yet, I think you would do better if the function did not rely on external variables, and did not exit (or generate output) by itself, but rather signaled back to the calling program.

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

inline int read_inputs_from_ip_if(FILE *ipf) {
    int input;
    if (EOF == fscanf(ipf,"%x\n",&input)) {
        return EOF;
    }
    return input;
}

int main() {
        int input;
        FILE *ipf;
        ipf = fopen("fscanf-data", "r");
        if (NULL == ipf) {
            fprintf(stderr, "File not found\n");
            return -1;
        }
        for(;;) {
                input = read_inputs_from_ip_if(ipf);
                if (EOF == input) {
                     // We just exit the loop.
                     break;
                }
                printf("Datum: 0x%02x\n", input);
        }
        printf("Terminated.\n");
        fclose(ipf);
        return 0;
}

Either way, output is as expected:

Datum: 0x01
Datum: 0x02
Datum: 0x04
Datum: 0x08
Datum: 0x0f
Datum: 0x1f
Datum: 0x2f
Datum: 0x4f
Terminated.

Upvotes: 1

Tezirg
Tezirg

Reputation: 1639

Use :

 fscanf(ipf,"%x\n",&input);

For reading line by line, because you omitted the end line character ('\n') Then, since the hexa values are formatted with upper case letters Use :

  fscanf(ipf,"%X\n",&input);

Upvotes: 2

Related Questions