Re: Design... the right way.



pete <pfiland@xxxxxxxxxxxxxx> writes:


#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAX_FIELDS 19
#define PATH_MAX 10

enum pkg_vars {
pkgname,
pkgver
};

int main(void)
/*
** non prototype declarations
** is an obsolescent feature of the language
*/
{
int lines = 0;
char package[MAX_FIELDS][PATH_MAX];
/*
** A two dimensional array of char
*/
FILE *input = fopen("./test.sh", "r");
char line[PATH_MAX + 1];

if(input == NULL) {
perror("Error: running parser");
/*
** Nothing to fclose here
*/
exit(EXIT_FAILURE);
}
while(fgets(line, PATH_MAX, input)) {
line[strlen(line) - 1 ] = '\0';
if(lines < MAX_FIELDS) {
strcpy(package[lines], line);
}
lines++;
}
printf("name = %s\n", package[pkgname]);
printf("pkgver = %s\n", package[pkgver]);
printf("total = %d\n", lines);
/*
** A portable text stream, ends in a newline character.
*/
fclose(input);
return 0;
}


Pete, thanks for your response. Thank you for for your suggestions, all
of them make sense. I have a couple of questionsi. When you say "non
prototype declarations" you mean I should declare void as a parmater, it
not to be assumed.

Also I'm not sure what you mean by a portable stream should end in a
newline. Does that mean I should'nt remove it from line or I should copy
the line then remove it?

Mike Rosset
.



Relevant Pages

  • Re: Design... the right way.
    ... int main ... ends in a newline character. ... Pete, thanks for your response. ... prototype declarations" you mean I should declare void as a parmater, ...
    (comp.lang.c)
  • Re: Design... the right way.
    ... int main ... ** A portable text stream, ends in a newline character. ... prototype declarations" you mean I should declare void as a parmater, ...
    (comp.lang.c)