Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
598 views
in Technique[技术] by (71.8m points)

file - How do I read hex numbers into an unsigned int in C

I'm wanting to read hex numbers from a text file into an unsigned integer so that I can execute Machine instructions. It's just a simulation type thing that looks inside the text file and according to the values and its corresponding instruction outputs the new values in the registers.

For example, the instructions would be:

  • 1RXY -> Save register R with value in memory address XY
  • 2RXY -> Save register R with value XY
  • BRXY -> Jump to register R if xy is this and that etc..
  • ARXY -> AND register R with value at memory address XY

The text file contains something like this each in a new line. (in hexidecimal)

  • 120F
  • B007
  • 290B

My problem is copying each individual instruction into an unsigned integer...how do I do this?

#include <stdio.h>
int main(){
    FILE *f;
    unsigned int num[80];

    f=fopen("values.txt","r");
    if (f==NULL){
        printf("file doesnt exist?!");
    }

    int i=0;
    while (fscanf(f,"%x",num[i]) != EOF){
        fscanf(f,"%x",num[i]);
        i++;
    }
    fclose(f);
    printf("%x",num[0]);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're on the right track. Here's the problems I saw:

  • You need to exit if fopen() return NULL - you're printing an error message but then continuing.
  • Your loop should terminate if i >= 80, so you don't read more integers than you have space for.
  • You need to pass the address of num[i], not the value, to fscanf.
  • You're calling fscanf() twice in the loop, which means you're throwing away half of your values without storing them.

Here's what it looks like with those issues fixed:

#include <stdio.h>

int main() {
    FILE *f;
    unsigned int num[80];
    int i=0;
    int rv;
    int num_values;

    f=fopen("values.txt","r");
    if (f==NULL){
        printf("file doesnt exist?!
");
        return 1;
    }

    while (i < 80) {
        rv = fscanf(f, "%x", &num[i]);

        if (rv != 1)
            break;

        i++;
    }
    fclose(f);
    num_values = i;

    if (i >= 80)
    {
        printf("Warning: Stopped reading input due to input too long.
");
    }
    else if (rv != EOF)
    {
        printf("Warning: Stopped reading input due to bad value.
");
    }
    else
    {
        printf("Reached end of input.
");
    }

    printf("Successfully read %d values:
", num_values);
    for (i = 0; i < num_values; i++)
    {
        printf("%x
", num[i]);
    }

    return 0
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...