Reputation: 683
I want to determine the stream size retruned by the popen() function call. I tried to use fseek and ftell but it returns the size as -1. Can anyone suggest me how to determine the file size? The following is the code what I am using....
char return_val[256];
FILE *fp = NULL;
char line[256];
memset (return_val, 0, 256);
/* set the defalut value */
strncpy (return_val, "N/A", 4);
char cmd[] = "if [ -f /etc/version ]; then cut -d, -f1 -s /etc/version ; fi";
/* Open the command for reading. */
fp = popen(cmd, "r");
if (fp != NULL)
{
/* read the line from file */
fgets (line, 256, fp);
if( line != NULL)
{
/* copy the data */
strncpy(return_val, line, strnlen (line, 256));
}
/* close the file */
pclose (fp);
}
Upvotes: 2
Views: 5262
Reputation: 162164
You can't. popen gives you a pipe, a FIFO First In First Out stream. Characters go in on the one side and they come out at the other. It is in the nature of a pipe that you don't know in advance how many bytes there will be transmitted.
You may either resort to in band signalling, how many bytes there are to come, or you implement buffering and dynamic allocation on your side.
Also seeking is not possible on a pipe, because its merely a kind of "Portal" between those processes. You know, like in the game "Portal"/"Portal2".
Upvotes: 6