1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
/* readln.c - Edward H. Flora - ehflora@ksu.edu */
/* Last Modified: 2/24/98 */
/*
* This function reads a string from a file pointed to by fp until a
* new line (EOLN) or an end of file is encountered. It returns a pointer
* to the string. The new line character is included in the string returned
* by the function.
*/
/* EXTERNALS:
******************
* Functions Called:
* Standard Libraries.
*
* Header Files Included:
* Standard Includes only.
*/
/* PORTABILITY:
******************
* Coded in ANSI C, using ANSI prototypes.
*/
/****** Include Files *************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define EOLN '\n' /* Defines the new line character */
#define SIZE1 10 /* Defines the increment to increase the */
/* string by until a newline of EOF is found */
/****** Function to Read a Line from fp *******************************/
char *readln(FILE *fp){
char *tmp, *t1, *t2;
int count = 0, s1 = SIZE1;
if((tmp = malloc(SIZE1*sizeof(char))) == NULL)return NULL;
/* If cannot allocate memory */
if((t1 = fgets(tmp, SIZE1, fp)) == NULL){
free(tmp);
return NULL;
} /* If there is no string to be read */
while(strchr(t1,EOLN) == NULL){ /* Loop while EOLN is not in the string */
count += strlen(t1);
if((s1 - count) > 1)break;
s1 +=SIZE1;
if((t2 = realloc(tmp, s1*sizeof(char))) == NULL){
free(tmp);
return NULL;
} /* If cannot allocate more memory */
else tmp = t2;
if((t1 = fgets((tmp+count),((s1 - count)),fp)) == NULL)break;
} /* End of While Loop */
if((t2 = realloc(tmp,strlen(tmp)+1)) == NULL)free(tmp);
return (t2);
}
/****** End of Function readln ****************************************/
|