I have a file that I open which has a bunch of lines with data like this:
0x804ae1c: W 0x0000000c
I'm trying to extract the 3 different strings into 3 different variables but I'm having problems getting values from the 2nd and third token.
Here is how I'm trying to do it:
unsigned long address;
unsigned long address2;
char* readWrite;
fscanf(traceFile, "%lx %s %lx\n", &address, line, &address2)
I get the pointer for the first line. i.e:
804ae1c
but after that the other 2 variables do not get the proper things assigned to them. I tried changing line to a char but to no avail. How should I proceed fixing this issue?
Thanks!
First, you need to allocate storage, rather than just declaring a char*
. If you are reading only a single character, a simple char
is sufficient.
As BLUEPIXY's comment mentions, you need to account for the extra :
character in your string. Here is a test program that shows this (reads from stdin).
#include <stdio.h>
int main(){
unsigned long address;
unsigned long address2;
char readWrite;
int retVal;
retVal = scanf("%lx: %c %lx", &address, &readWrite, &address2);
if (retVal == 3) {
printf("%lu: %lu %c\n", address, address2, readWrite);
} else {
fprintf(stderr, "Parsing error\n");
}
}
User contributions licensed under CC BY-SA 3.0