I am working on a project where I will have either read or write commands and then an address on a single line in a file. The format will be as follows:
R 0x...
W 0x...
And the file is thousands of lines long. I am attempting to read the command and put it in buf1
and read the address into buf2
. I have attempted to do this with fgets
/ fgetc
, as well as fscanf
with "%*c %s"
and the other way around. Each time I do this, buf2
will grab the address correctly but the command is very hit or miss. Here is my code:
char buf1, buf2[64];
int readcount = 0, writecount = 0, other = 0;
while(!feof(trace)){
printf("\nFile is open");
fgetc(trace);
fgets(buf2,16,trace);
printf("\nBuf1 is: %c",buf1);
printf("\nBuf2 is: %s",buf2);
and the output that I keep getting is:
The address is: E
File is open
Buf1 is: ?
Buf2 is: 0x123456
The address is: V
File is open
Buf1 is: ?
Buf2 is: 0x234567
The address is: g
File is open
Buf1 is: ?
Buf2 is: 0x345678
The address is: x
File is open
Buf1 is: ?
Buf2 is: 0x345678
I have a feeling that the problem is my understanding of the file-reading functions. Why isn't buf1 reading correctly?