user3248730
user3248730

Reputation: 1

Problems with fscanf and numbers stored on file in native int format, not text

Data at start of file in HEX 78000000 0300497B ..............

equating to int32 120 and char 03 followed by a load of other char data,

data written by another of my programs and viewed in hex dump mode.

When trying to read it back with another program I have tried..

int j,padNumber;
char rot;

j=fscanf(fp,"%d%c",&padNumber,&rot);  // insists on returning j=0,padNumber=0 & rot=0

whereas

char c1,c2,c3,c4,rot;

j=fscanf(fp,"&c&c&c&c&c",&c1,%c2,&c3,&c4,&rot);// gives 

j=5,c1='x',c2='\0',c3='\0',c4='\0',rot='!x03'

which equates to my on file data.

Why can't I get my int back in native format

Upvotes: 0

Views: 109

Answers (1)

SzG
SzG

Reputation: 12629

Use fread when reading from a native binary dump.

fread(&padNumber, sizeof padNumber, 1, fp);
fread(&rot, sizeof rot, 1, fp);

Or if you're on a Unix platform, the direct read syscall could work too.

#include <unistd.h>
...
read(fd, &padNumber, sizeof padNumber);
read(fd, &rot, sizeof rot);

Upvotes: 3

Related Questions