Thread: get length of a stream

  1. #1
    Registered User
    Join Date
    May 2010

    get length of a stream

    Hi all.
    I want get the length of a stdin. i.e

     cat test.txt | myprogram
    the program myprogram reads the test.txt and returns the length of the file test.txt

    FILE* stdin_stream = fdopen(STDIN_FILENO,"r");
    //now i get the length
    length = ftell(stdin_stream);
    but i have always length = -1....

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    Yeah, I dunno if that is how I would approach stdin. I'd start at the beginning and count the characters to EOF.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jun 2010
    To get the length of "test.txt" file:
    FILE *stdin_stream=fopen("test.txt","r");

    // the file must exit and be initialized first in the current directory.

    printf("The length of file is %d\n",ftell(stdin_stream));

    //Hi,mp2k.maybe you should change the function fdopen() to fopen().The result "-1" means that the error occurs in function ftell(), because you hasn't input any characters to the standard input terminal.Right?I am steve.
    Last edited by Steve Cao; 06-22-2010 at 05:06 AM.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    stdin is a stream, so it isn't seekable. If you want to know its length, you must read data until you get EOF. If you need to DO something with that data, you need to store it somehow since it will be lost as you read it.

    One possibility is to copy the data to a temporary file as you read it in. Then you can open this file and seek in it.
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

  5. #5
    Registered User
    Join Date
    Mar 2009
    A simple solution would be:

    char buff[1024];
    int len = 0;
    //Open the filename passed as argument int the main (argv[])
    FILE *file = fopen(filename.txt, "r");    //open file
    while(fgets(buff, 1024, file) != NULL) {  //read each line untill eof
         len += strlen(buff);    //add the number of characters of each line
    printf("Size of file is: %d\n", len);  //print the size as the result

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Weird modification to string length
    By ChwanRen in forum C Programming
    Replies: 0
    Last Post: 08-17-2003, 10:45 AM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM