Re: adapting getline
- From: Franken Sense <frank@xxxxxxxxxxxxxxx>
- Date: Sun, 19 Apr 2009 17:27:04 -0700
In Dread Ink, the Grave Hand of kid joe Did Inscribe:
On Sun, 19 Apr 2009 15:58:02 -0700, Franken Sense wrote:
It's very simple to use, for example:
These suggestions were enough:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define my_file "text42.txt"
void *getline(FILE *);
int main(void)
{
FILE *fp;
char *line;
int n = 0;
double len = 0;
if ((fp = fopen(my_file, "r")) == NULL ) {
fprintf(stderr, "can't open file\n");
exit(EXIT_FAILURE);
}
while ((line = getline(fp))) {
n++;
len += strlen(line);
printf(line);
free(line);
}
printf("There were %d lines, average length %f\n", n, len / n);
fclose(fp);
return (0);
}
// returns a NULL-terminated line, caller should free buffer,
// returns NULL for end of file or error
void *getline(FILE * fd)
{
char *buf = NULL;
int c = 1, bufsz = 0;
while (c && (c = fgetc(fd)) != EOF) {
LOOP:
buf = realloc(buf, ++bufsz);
*(buf + bufsz - 1) = c;
if (c == '\n') {
c = 0;
goto LOOP;
}
}
return buf;
}
// gcc ben7.c -Wall -o out
// end source begin abridged output
E:\gfortran\dan>out
ALLUSERSPROFILE=C:\Documents and Settings\All Users
APPDATA=C:\Documents and Settings\dan\Application Data
CLIENTNAME=Console
....
USERNAME=dan
USERPROFILE=C:\Documents and Settings\dan
windir=C:\WINDOWS
There were 32 lines, average length 38.125000
E:\gfortran\dan>
I really like working something up a few different ways, for when, for
example, I need variable buffers. One question here:
printf("There were %d lines, average length %f\n", n, len / n);
I wouldn't think an integer divided by an integer would behave like it
does(?)
realloc is, in my book, dangerous for programmers and not to be used if you
can avoid it, which, for my current purposes, I can. I'm inputting text.
It's not text if it's over a hundred characters long per line, it's
calligraphy or a long vector of letters.
No, it's much safer using realloc than fixedsize buffers. Check out
"buffer overflows" on Wikipedia!
My counterargument is that if realloc can blow up in Heathfield's face,
then it can do so to anyone. Persons who have had similar difficulty
comprise a who's who of clc. Maybe this use is an example of something
safe with realloc.
--
Frank
I think some people hold [G.W.Bush] in high esteem because they watch Fox.
And they get their news from Rush Limbaugh. And they are fooled.
~~ Al Franken, in response to the 2004 SOTU address
.
- Follow-Ups:
- Re: adapting getline
- From: kid joe
- Re: adapting getline
- From: Richard Heathfield
- Re: adapting getline
- From: Ben Bacarisse
- Re: adapting getline
- From: CBFalconer
- Re: adapting getline
- References:
- adapting getline
- From: Franken Sense
- Re: adapting getline
- From: kid joe
- Re: adapting getline
- From: Franken Sense
- Re: adapting getline
- From: kid joe
- adapting getline
- Prev by Date: Re: adapting getline
- Next by Date: Re: memory allocation question
- Previous by thread: Re: adapting getline
- Next by thread: Re: adapting getline
- Index(es):
Relevant Pages
|