-
Notifications
You must be signed in to change notification settings - Fork 2
/
io.c
46 lines (42 loc) · 1.1 KB
/
io.c
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
#include <stdio.h>
#include <malloc.h>
#include "io.h"
/**
* This function returns a struct containing the file's (fp)
* number of lines, and the length of the longest line
*/
f_i get_file_info(FILE *fp, f_i * fi){
char c;
int char_count = 0;
int lines = 0;
int max = 0;
*fi = (f_i) malloc(sizeof(struct FILE_INFO));
while((c = getc(fp)) != EOF){
if(c == '\n'){
++lines;
++char_count; //always accounting for the new line character
++char_count; //and accounting for the longest line ending char
if(char_count > max){
max = char_count;
}
char_count = 0;
} else {
char_count++;
}
}
(*fi)->max_line_length = max;
(*fi)->lines = ++lines;
return *fi;
}
char * get_line(char * line, int max_line, FILE * f) {
register int c;
register char * str;
str = line;
while(--max_line > 0 && (c = getc(f)) != EOF) {
*str++ = c;
if(c == '\n')
break;
}
*str = '\0';
return (c == EOF && str == line) ? NULL : line;
}