ela said:
Is there any similar structure in C that I can easily get the strings in a
file like:
Example file
bicycle
bus
ferry
plane
train
so I can print them generically? In Perl, I just read them into an array,
e.g. @vehicle
then get them back by foreach. Thanks for telling me if there's similar
handy function in C
#include <stdio.h>
#include <stdlib.h>
#define LINSIZ 100
#ifndef __LCC__
// This program uses the fggets utility of C.B. Falconer. Here
// is the (slightly modified) code for people that do not use
// the lcc-win compiler or do not have it
#define INITSIZE 112 /* power of 2 minus 16, helps malloc */
#define DELTASIZE (INITSIZE + 16)
enum {OK = 0, NOMEM};
int EXPORT fggets(char* *ln, FILE *f)
{
int cursize, ch, ix;
char *buffer, *temp;
*ln = NULL; /* default */
if (NULL == (buffer = malloc(INITSIZE))) return NOMEM;
cursize = INITSIZE;
ix = 0;
while ((EOF != (ch = getc(f))) && ('\n' != ch)) {
if (ix >= (cursize - 1)) { /* extend buffer */
cursize += DELTASIZE;
if (NULL == (temp = realloc(buffer, (size_t)cursize))) {
/* ran out of memory, return partial line */
buffer[ix] = '\0';
*ln = buffer;
return NOMEM;
}
buffer = temp;
}
buffer[ix++] = ch;
}
if ((EOF == ch) && (0 == ix)) {
free(buffer);
return EOF;
}
buffer[ix] = '\0';
if (NULL == (temp = realloc(buffer, (size_t)ix + 1))) {
*ln = buffer; /* without reducing it */
}
else *ln = temp;
return OK;
} /* fggets */
#endif
/*------------------------------------------------------------------------
Procedure: FileToArray
Purpose: Reads from the given file all the lines in it and
returns a pointer to an array of lines. In the given
int pointer returns the number of lines read, or an
error code if an error happened.
Input: Name of the file to process
Pointer to an integer that receives the number of
lines read or an error code, always negative
Output: A table of lines or NULL if error
Errors: -1 if file could not be opened, or a negative number
of lines if a memory error happened after reading
some lines
------------------------------------------------------------------------*/
char **FileToArray(char *file_name,int *linesread)
{
FILE *f = fopen(file_name,"r");
int line_counter=0,allocated_lines=0;
char **line_array=NULL,*linebuf=NULL;
*linesread = 0;
if (f == NULL) {
*linesread = -1;
return NULL;
}
while ((fggets(&linebuf,f)) == 0) {
if (line_counter >= allocated_lines) {
char **tmp = realloc(line_array,
sizeof(char *)*(line_counter+LINSIZ));
if (tmp == NULL) {
if (line_counter > 0)
line_counter = -line_counter;
break;
}
else line_array = tmp;
allocated_lines += LINSIZ;
}
line_array[line_counter++] = linebuf;
}
fclose(f);
*linesread = line_counter;
if (line_counter < allocated_lines) {
line_array = realloc(line_array,sizeof(char *)*line_counter);
}
return line_array;
}
#ifdef TEST
int main(int argc,char *argv[])
{
int lines;
char **plines;
if (argc < 2) {
return -1;
}
plines = FileToArray(argv[1],&lines);
if (plines) {
printf("%d lines\n---\n",lines);
for (int i=0; i<lines;i++) {
printf("%s\n",plines
);
}
}
else {
if (lines == -1)
printf("file not found\n");
else printf("memory error after reading %d lines\n",-lines);
}
}
#endif