halam2@umn-d-ub.D.UMN.EDU (Haseen I. Alam) (06/12/90)
I am using BSD Unix 4.2 on Sun 3's, a Sparc, and on a Encore multimax. I do not think this is a machine dependent question, but this info is for anyone who might flame me later for not specifing it. 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? This method has a certain drawback. I had to hard code the filename. I would need to be able to use different filenames from the command line and get similar info for further processing. Is there any variations to the system command that I am not aware of? Is there a built in C function that will do this. 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. I do not mean to waste bandwidth with questions that have recently been answered. I read this group regularly. There was some postings about a compilation of frequently asked questions. Where can I access that info? Any helpful gesture will be highly appreciated. Thanks. Haseen. -- .--------------------------------------------------------------------. | Haseen Ibne Alam Tel: (218)-728-2139 | | email : halam1@ub.d.umn.edu or halam@cola.d.umn.edu | `--------------------------------------------------------------------'
ghoti+@andrew.cmu.edu (Adam Stoller) (06/12/90)
Excerpts from netnews.comp.lang.c: 12-Jun-90 How to read in file size
Haseen I. Alam@umn-d-ub. (1555)
I am using BSD Unix 4.2 on Sun 3's, a Sparc, and on a Encore multimax. .....
I am aware of the wc (word count) program in unix......
> .....But I need to read that into my program.... Is there a built in C
function that will do this.
Any helpful gesture will be highly appreciated. Thanks.
Haseen.
In BSD (at least) - look up the function 'stat' and its structure field
'st_size'.
--fish
harp@cadillac.CAD.MCC.COM (Christopher North-Keys) (06/12/90)
> I am using BSD Unix 4.2 on Sun 3's, a Sparc, and on a Encore multimax. I > do not think this is a machine dependent question, but this info is for > anyone who might flame me later for not specifing it. > > 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") ; No! (blech!) See the library function "fstat". A code fragment from SunOS follows. The reference to the file size is the line: if((*addr_buffer=(char *)calloc(1, statbuf.st_size+1))==NULL) ^^^^^^^^^^^^^^^ #------------------------------------------------------------------------- /* I haven't check this include-list for non-essentials */ #include <stdio.h> #include <sys/file.h> #include <sys/types.h> #include <sys/stat.h> #include <ctype.h> #include "stdelf.h" long SwallowFile(addr_buffer, path) /* North-Keys, 1989 */ char **addr_buffer, *path; { struct stat statbuf; int in_count; int fd = -1; /* Attempt to open desired file */ if(((fd)=open(path, O_RDONLY))==-1) { (void)fprintf(stderr, "SwallowFile: %s not opened\n", path); return (FAIL); } if(fstat(fd, &statbuf)==-1) { (void)fprintf(stderr, "SwallowFile: error on fstat file %s\n", path); return (FAIL); } /* Get a buffer to fit */ if((*addr_buffer=(char *)calloc(1, statbuf.st_size+1))==NULL) { (void)fprintf(stderr, "SwallowFile: no space for calloc\n"); return (FAIL); } /* Read in the file */ if((in_count = read(fd, *addr_buffer, statbuf.st_size)) != statbuf.st_size) { (void)fprintf(stdout, "SwallowFile: error(?) %d/%d bytes read from %s\n", in_count, statbuf.st_size, path); (void)free(*addr_buffer); return (FAIL); } return (in_count); } -- --Christopher Alexander North-Keys----/\-------------------------------------- Co-founder Group Talisman / \/\ ^*^ Harp[@Mcc.Com] [*my* opinions] / \ \ Associate Systems Analyst --------------------------Microelectronics & Computer Technology Corporation--
darcy@druid.uucp (D'Arcy J.M. Cain) (06/13/90)
In article <3537@umn-d-ub.D.UMN.EDU> halam2@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@druid) | Government: D'Arcy Cain Consulting | Organized crime with an attitude West Hill, Ontario, Canada | (416) 281-6094 |