-
Notifications
You must be signed in to change notification settings - Fork 2
/
line_io.c
79 lines (70 loc) · 1.67 KB
/
line_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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*------------------------------------------------------
*
* line_io.c
*
* this file contains contains two functions, one is input one
* line data from file and another is output one line data to file
*
* Project : LyreBird
* Name : Chong Guo
* Student ID : 301295753
* SFU username : armourg
* Lecture section : D1
* Instructor : Brain G.Booth
* TA : Scott Kristjanson
*
* Created by Armour on 15/09/2015
* Copyright (c) 2015 Armour. All rights reserved.
*
*------------------------------------------------------
*/
#include "line_io.h"
#include "memwatch.h"
/*
* Function: input_line
* -------------------
* Get each line tweet from file
*
* Parameters:
* fin: pointer to input file
*
* Returns:
* one line tweet content
*/
char *input_line(FILE *fin, int *len) {
char c;
char *line;
int i = 0;
line = (char *)malloc(sizeof(char) * (TWEETS_MAX_LENGTH));
if (line == NULL) { /* If malloc error */
return line;
}
c = fgetc(fin);
while (c != EOF) {
line[i++] = c;
if (c == '\n') break; /* Break when meet new line */
c = fgetc(fin);
}
*len = i - 1;
return line;
}
/*
* Function: output_line
* -------------------
* Output each line decryped tweet to file
*
* Parameters:
* fout: pointer to output file
* tweet: decrypted tweet content
* len: length of decrypted tweet
*
* Returns:
* void
*/
void output_line(FILE *fout, char *tweet, int len) {
int i;
for (i = 0; i < len; ++i) {
fprintf(fout, "%c", tweet[i]);
}
fprintf(fout, "\n");
}