How to read in file size

D'Arcy J.M. Cain darcy at druid.uucp
Wed Jun 13 22:38:44 AEST 1990


In article <3537 at umn-d-ub.D.UMN.EDU> halam2 at umn-d-ub.D.UMN.EDU (Haseen I. Alam) writes:
> I am aware of the wc (word count) program in unix.  I can use something
> like this in my C program.....
>       system ("wc -c filename") ;
>
> This will execute the wc command and print out the info out to the terminal.
> But I need to read that into my program.  How can I make this work?
Use popen(3S) instead.  You then simply read and parse the input from the file
handle returned.

>  This
> method has a certain drawback.  I had to hard code the filename.  I would
>
Use sprintf to build up the string you want to create as follows:
    sprintf(cmd_str, "wc -c %s", filename);
    fp = popen(cmd_str, "r");

> Now speed is a concern in what I am doing.  I can not open each file at a
> time and count up the characters.  Files are big and I use 2 to 5 of them at
> once.
>
If speed is a concern then you should open the files yourself.  After all
that's what wc has to do and you are just adding the system or popen call
overhead to that.  Also if all you want is the number of characters in the
file try the stat(2) function to get the file size.

-- 
D'Arcy J.M. Cain (darcy at druid)     |   Government:
D'Arcy Cain Consulting             |   Organized crime with an attitude
West Hill, Ontario, Canada         |
(416) 281-6094                     |



More information about the Comp.lang.c mailing list