I've a file RTCM3.bin. When I convert this file into hexadecimal characters, I can see its content. For example:
0x3A, 0x00, 0x04, 0x10, 0xD3, 0x00, 0x95, 0x3E, 0xC0, 0x01, 0x49, 0x66, 0xA6, 0x42, 0x90, 0x09, 0x2E, 0x0D, 0x8E, 0x00, 0x30, 0x86, 0x14, 0x68, 0x70 etc. etc.
The problem is that this characters are passed to a function:
FILE *p;
p = fopen("./RTCM3.bin", "r");
int y = fgetc(p);
printf("First character in input to input_rtcm3f: %d\n", y);
int x = input_rtcm3f(&rtcm, p);
printf("Return value of input_rtcm3f: %d\n", x);
Anyway, I need to provide in input to the input_rtcm3 function only the characters from 0xD3 onwards. In this case:
`0xD3, 0x00, 0x95, 0x3E, 0xC0, 0x01, 0x49` etc. etc.
How can I do it? y is the integer value of 0xD3, that is 211. So, y should be 211 I think.
CodePudding user response:
fseek is what you are looking for:
https://www.tutorialspoint.com/c_standard_library/c_function_fseek.htm
With fseek you can move the file pointer to the correct position if you know the offset.
Otherwise you would have to loop to the correct character while consuming from the stream with fgetc.
CodePudding user response:
If you know the offset (e.g.: you're skipping a standard header and such) then you should use fseek to move the pointer in the file to the right position.
If you don't know the offset then you'll have to find it yourself - read the file until you get to the place you want in a loop, and then call the function once you're there (you may need to use fseek to move back one position).
CodePudding user response:
Generally, you have two options to solve this problem:
Read all bytes with
fgetcin a loop, until you encounter the byte with the value0xD3.If you know the offset of the byte with the value
0xD3, jump directly to that offset using the functionfseek.
However, option #2 has the following problem:
In ISO C, calling fopen with the mode argument "r" will open the file in text mode. In that mode, you are only allowed to pass offset values previously returned by the function ftell. You cannot simply pass the offset value 4.
However, in your case, since the input file seems to be a binary file, it would probably be better to open the file in binary mode instead of text mode, by passing "rb" as the mode argument to fopen. That way, you can simply pass the offset 4 to fseek.
Note that some operating systems such as Linux do not distinguish between text mode and binary mode. When using such operating systems, you can simply pass the offset 4 to fseek, even if you opened the file with the mode "r".
