Re: adapting getline
- From: Franken Sense <frank@xxxxxxxxxxxxxxx>
- Date: Sun, 19 Apr 2009 15:21:04 -0700
In Dread Ink, the Grave Hand of Eric Sosman Did Inscribe:
Franken Sense wrote:
Change getline() so it takes a third parameter, the
FILE* stream to read from, and use getc(the_stream_param)
instead of getchar(). In the call to the new getline(),
pass the value of `fp' as the argument matching the new
parameter.
That's got it:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define my_file "text42.txt"
#define NUMBER 100
#define MAXFMTLEN 200
int getline(FILE *, char *, int );
int main(void)
{
FILE *fp;
char text[NUMBER];
unsigned long lineNumber = 0;
int len;
if ((fp = fopen(my_file, "r")) == NULL ) {
fprintf(stderr, "can't open file\n");
exit(EXIT_FAILURE);
}
while((len = getline(fp, text, NUMBER)) > 0) {
++ lineNumber,
printf("%lu, %s", lineNumber, text);
}
fclose(fp);
return 0;
}
/* getline: get line into s, return length */
int getline(FILE *fp, char *s, int lim)
{
char *p;
int c;
p = s;
while (--lim > 0 && (c = getc(fp)) != EOF && c != '\n')
*p++ = c;
if (c == '\n')
*p++ = c;
*p = '\0';
return (p - s);
}
// gcc ben6.c -Wall -o out
To get a useful warning about a potential problem in
getline(), crank up gcc's diagnostic level and call for an
optimization level that prompts the compiler to study the
usage patterns of variables. `-W -Wall -ansi -pedantic -O2'
is a reasonable starting point.
What would I be asking gcc.exe to do with these warnings?
To cure the error that will occur after you get the code
compiled and try to run it, see Question 7.1 in the comp.lang.c
Frequently Asked Questions (FAQ) list at <http://www.c-faq.com/>.
I think I've got that covered. It shows the proper use of fgets:
#include <stdio.h>
#include <string.h>
char answer[100], *p;
printf("Type something:\n");
fgets(answer, sizeof answer, stdin);
if((p = strchr(answer, '\n')) != NULL)
*p = '\0';
printf("You typed \"%s\"\n", answer);
To forestall the accusation that you're re-inventing the
wheel, explain why you're writing your own getline() instead
of using fgets() and perhaps strlen(), both of which are
already written for you.
I'm following the development in K&R and am relying heavily on the
solutions to these that I can find. Any suggestions for sleeker code are
welcome.
It seemed complicated at dinkumware:
fgets
char *fgets(char *restrict s, int n, FILE *restrict stream);
The function reads characters from the input stream stream and stores them
in successive elements of the array beginning at s and continuing until it
stores n-1 characters, stores an NL character, or sets the end-of-file or
error indicators. If fgets stores any characters, it concludes by storing a
null character in the next element of the array. It returns s if it stores
any characters and it has not set the error indicator for the stream;
otherwise, it returns a null pointer. If it sets the error indicator, the
array contents are indeterminate.
--
Frank
That's just stinkin' thinkin!
~~ Al Franken,
.
- Follow-Ups:
- Re: adapting getline
- From: Eric Sosman
- Re: adapting getline
- References:
- adapting getline
- From: Franken Sense
- Re: adapting getline
- From: Eric Sosman
- adapting getline
- Prev by Date: Re: adapting getline
- Next by Date: Re: adapting getline
- Previous by thread: Re: adapting getline
- Next by thread: Re: adapting getline
- Index(es):
Relevant Pages
|