c - Using fseek with a file pointer that points to stdin -


depending on command-line arguments, i'm setting file pointer point either towards specified file or stdin (for purpose of piping). pass pointer around number of different functions read file. here function getting file pointer:

file *getfile(int argc, char *argv[]) {     file *myfile = null;     if (argc == 2) {         myfile = fopen(argv[1], "r");         if (myfile == null)            fprintf(stderr, "file \"%s\" not found\n", argv[1]);     }     else         myfile = stdin;     return myfile; } 

when it's pointing stdin, fseek not seem work. that, mean use , use fgetc , unexpected results. expected behavior, , if so, how move different locations in stream?

for example:

int main(int argc, char *argv[]) {     file *myfile = getfile(argc, argv); // assume pointer set stdin     int x = fgetc(myfile); // expected result     int y = fgetc(myfile); // expected result     int z = fgetc(myfile); // expected result      int foo = bar(myfile); // unexpected result      return 0; }  int bar(file *myfile) {     fseek(myfile, 4, 0);     return fgetc(myfile); } 

yes, it's normal fseek won't work on stdin -- it'll work on disk file, or reasonably similar.

though it's posix thing, can typically use if (isatty(fileno(myfile))) @ least pretty idea of whether seeking work in particular file. in cases, isatty and/or fileno have leading underscore (e.g., iirc versions provided microsoft's compilers do).


Comments

Popular posts from this blog

Delphi Wmi Query on a Remote Machine -