problem with reading struct from file generated by hexdump
Hi,
I am new to the forum and also a rookie C-programmer.
So please excuse if I will ask a typical beginner question to you but this problem drives me nuts for days now.
Here is the problem:
I want to read a struct from a file.
When I do this as an experiment:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int one;
} record ;
record weather;
record new_weather;
FILE *fp;
int i;
int main (void)
{
record *weather_ptr,*weather_ptr_start,*new_weather_ptr;
weather_ptr =&weather;
weather_ptr_start =&weather;
new_weather_ptr =&new_weather;
for (i=1;i<=10;i++)
{ weather_ptr->one = i;
weather_ptr++;
}
fp =fopen("test.txt","w");
weather_ptr = weather_ptr_start;
for (i=1;i<=10;i++)
{
fprintf(fp, "%u\n",weather_ptr->one);
weather_ptr++;
}
fclose(fp);
fp =fopen("test.txt","r");
for (i=1;i<=10;i++)
{
fscanf(fp, "%u\n",&new_weather_ptr->one);
printf("%u\n",new_weather_ptr->one);
weather_ptr++;
}
return 0;
}
All goes well.
So I think (I think!) I have understood the mechanics of pointers, structs and file IO.
Now I have generated a file from a binary with hexdump (unix)
The file when looked at with vi looks OK and similar to what I have generated with above test program.
But if I run the read part of the above program I get a crash.
It happens to crash when I try to read the second line of the text file.
The first struct record is OK.
What I see is that the first line gets read in correct and the file pointer jumps to the next line.
The next fscanf will then generate the crash.
Do I have a misunderstanding of how hexdump works?
Is there any special character which I do not se and which will crash fscanf?
Thanks for your help.