1 /* readln.c - Edward H. Flora - ehflora@ksu.edu */
2 /* Last Modified: 2/24/98 */
4 * This function reads a string from a file pointed to by fp until a
5 * new line (EOLN) or an end of file is encountered. It returns a pointer
6 * to the string. The new line character is included in the string returned
15 * Header Files Included:
16 * Standard Includes only.
21 * Coded in ANSI C, using ANSI prototypes.
24 /****** Include Files *************************************************/
29 #define EOLN '\n' /* Defines the new line character */
30 #define SIZE1 10 /* Defines the increment to increase the */
31 /* string by until a newline of EOF is found */
33 /****** Function to Read a Line from fp *******************************/
34 char *readln(FILE *fp
){
36 int count
= 0, s1
= SIZE1
;
38 if((tmp
= malloc(SIZE1
*sizeof(char))) == NULL
)return NULL
;
39 /* If cannot allocate memory */
40 if((t1
= fgets(tmp
, SIZE1
, fp
)) == NULL
){
43 } /* If there is no string to be read */
44 while(strchr(t1
,EOLN
) == NULL
){ /* Loop while EOLN is not in the string */
46 if((s1
- count
) > 1)break;
48 if((t2
= realloc(tmp
, s1
*sizeof(char))) == NULL
){
51 } /* If cannot allocate more memory */
53 if((t1
= fgets((tmp
+count
),((s1
- count
)),fp
)) == NULL
)break;
54 } /* End of While Loop */
55 if((t2
= realloc(tmp
,strlen(tmp
)+1)) == NULL
)free(tmp
);
58 /****** End of Function readln ****************************************/