convert between FILE type and filedescriptor in C

78 views Asked by At

LANG: C \ OS: Linux

I am writing a streaming program, I already implemented Play, stop and pause functionlities, when I came to implement seek, I got into the following problem and I got stuck.

For opening the file before streaming I am using the following method:

int transport_fd = open(tsfile, O_RDONLY);
if(transport_fd < 0) 
                {
                    fprintf(stderr, "can't open file %s\n", tsfile);
                    close(sockfdstr); // using this to stream 
                }
// .....
//.....
len = read(transport_fd, send_buf, packet_size); // here, the read function takes fd as a first argument.

To use fseek() function, I should pass a pointer of type FILE as an opened file to seek into, i.e pass a file pointer to the already opened stream, so how should I handle this?

Sample dummy code:

FILE *fp;
{CODE TO CONVERT BETWEEN *fp AND tsfd - OR ANY OTHER FIX TO THIS SITUATION}
tsfd = fseek(fp, offset, SEEK_CUR);
len = read(tsfd, send_buf, packet_size);
1

There are 1 answers

2
cfz42 On BEST ANSWER

I think you can use the lseek function for your purpose. You already used open and read instead of fopen and fread, just use lseek instead of fseek.