I am trying to dynamically allocate memory for output of popen but trying to find the length of the file is giving "illegal seek" error. The code is
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char cmd[40] = {0};
char * c;
int byteCount = 0;
FILE * pFp = NULL;
strncpy(cmd, "ls -l", sizeof(cmd));
pFp = popen(cmd, "r");
if (pFp == NULL)
printf ("file is null");
fseek(pFp, 0, SEEK_END);
byteCount = ftell(pFp);
perror("seek ");
fclose(pFp);
}
fgets works fine, are there any constraints in using fseek with popen? Thanks in advance.
Yes you can't seek backwards from a pipe, only forwards. A pipe in the computer is just like a pipe in real life, the data flows only in one direction, and you can't change the flow.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With