I want to read hexadecimal numbers from a text file into an unsigned integer so that I can follow machine instructions. It is simply a type of simulation type that scans a text file and according to the values, and the corresponding command outputs the new values to the registers.
For example, the instructions will be:
- 1RXY → Save register R with value to memory address XY
- 2RXY → Save register R with value XY
- BRXY → Go to register R, if xy is it, etc.
- ARXY → And register R with value in memory address XY
The text file contains something like this in a new line. (in hexadecimal)
My problem is copying every single instruction into an unsigned integer ... how to 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]);
}