Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1010449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:05:05+00:00 2026-05-16T09:05:05+00:00

I am writing a utility which accepts either a filename, or reads from stdin.

  • 0

I am writing a utility which accepts either a filename, or reads from stdin.

I would like to know the most robust / fastest way of checking to see if stdin exists (data is being piped to the program) and if so reading that data in. If it doesn’t exist, the processing will take place on the filename given. I have tried using the following the test for size of stdin but I believe since it’s a stream and not an actual file, it’s not working as I suspected it would and it’s always printing -1. I know I could always read the input 1 character at a time while != EOF but I would like a more generic solution so I could end up with either a fd or a FILE* if stdin exists so the rest of the program will function seamlessly. I would also like to be able to know its size, pending the stream has been closed by the previous program.

long getSizeOfInput(FILE *input){
  long retvalue = 0;
  fseek(input, 0L, SEEK_END);
  retvalue = ftell(input);
  fseek(input, 0L, SEEK_SET);
  return retvalue;
}

int main(int argc, char **argv) {
  printf("Size of stdin: %ld\n", getSizeOfInput(stdin));
  exit(0);
}

Terminal:

$ echo "hi!" | myprog
Size of stdin: -1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T09:05:06+00:00Added an answer on May 16, 2026 at 9:05 am

    First, ask the program to tell you what is wrong by checking the errno, which is set on failure, such as during fseek or ftell.

    Others (tonio & LatinSuD) have explained the mistake with handling stdin versus checking for a filename. Namely, first check argc (argument count) to see if there are any command line parameters specified if (argc > 1), treating - as a special case meaning stdin.

    If no parameters are specified, then assume input is (going) to come from stdin, which is a stream not file, and the fseek function fails on it.

    In the case of a stream, where you cannot use file-on-disk oriented library functions (i.e. fseek and ftell), you simply have to count the number of bytes read (including trailing newline characters) until receiving EOF (end-of-file).

    For usage with large files you could speed it up by using fgets to a char array for more efficient reading of the bytes in a (text) file. For a binary file you need to use fopen(const char* filename, "rb") and use fread instead of fgetc/fgets.

    You could also check the for feof(stdin) / ferror(stdin) when using the byte-counting method to detect any errors when reading from a stream.

    The sample below should be C99 compliant and portable.

    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    
    long getSizeOfInput(FILE *input){
       long retvalue = 0;
       int c;
    
       if (input != stdin) {
          if (-1 == fseek(input, 0L, SEEK_END)) {
             fprintf(stderr, "Error seek end: %s\n", strerror(errno));
             exit(EXIT_FAILURE);
          }
          if (-1 == (retvalue = ftell(input))) {
             fprintf(stderr, "ftell failed: %s\n", strerror(errno));
             exit(EXIT_FAILURE);
          }
          if (-1 == fseek(input, 0L, SEEK_SET)) {
             fprintf(stderr, "Error seek start: %s\n", strerror(errno));
             exit(EXIT_FAILURE);
          }
       } else {
          /* for stdin, we need to read in the entire stream until EOF */
          while (EOF != (c = fgetc(input))) {
             retvalue++;
          }
       }
    
       return retvalue;
    }
    
    int main(int argc, char **argv) {
       FILE *input;
    
       if (argc > 1) {
          if(!strcmp(argv[1],"-")) {
             input = stdin;
          } else {
             input = fopen(argv[1],"r");
             if (NULL == input) {
                fprintf(stderr, "Unable to open '%s': %s\n",
                      argv[1], strerror(errno));
                exit(EXIT_FAILURE);
             }
          }
       } else {
          input = stdin;
       }
    
       printf("Size of file: %ld\n", getSizeOfInput(input));
    
       return EXIT_SUCCESS;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a utility in Java that reads a stream which may contain
I'm writing an utility ( http://reg2run.sf.net ) which in case execution without arguments works
I'm writing a VBScript for SyncBack (backup utility) in which I am deleting old
I'm writing a utility for myself, partly as an exercise in learning C# Reflection
I'm writing a utility in Python that will attach changed files in Subversion to
I am writing a Java utility that helps me to generate loads of data
I'm writing an import utility that is using phone numbers as a unique key
I'm writing some db utility scripts, and one of the tasks I need to
I'm writing a little utility that starts with selecting a file, and then I
I am writing a Python utility that needs to parse a large, regularly-updated CSV

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.