I want to read some number K of bytes into a binary file starting at position P and save it to another file.
I think I should make use of the fseek tool but I'm not sure, any light would help. Thank you
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fichero_in;
fichero_in = fopen("test.bin","rb");
FILE *fichero_out;
fichero_out = fopen("copia.bin","wb");
int k = 10;
int p = 50;
char *buf;
buf = malloc(k);
//¿como hago para leer a partir de la posicion 50?
// aqui lee desde el inicio.
fread(buf, 1, k, fichero_in );
fwrite(buf,1, k, fichero_out);
fclose(fichero_in);
fclose(fichero_out);
free(buf);
}
fseek
Defined in the header
int fseek( FILE *stream, long offset, int source );
sets the stream position of the file indicated by stream to the value pointed to by offset
Parameters
stream - stream file to modify offset - number of characters to roll from position relative to origin origin - position to which offset will add values; can have one of the following values
_SEEK_SET:_ From start of file _SEEK_CUR:_ From current position _SEEK_END:_ From end of file
in my case the solution was:
With information translated from fseek