-
Notifications
You must be signed in to change notification settings - Fork 10
/
io.cpp
106 lines (87 loc) · 2.46 KB
/
io.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <sys/timeb.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#define MAX_LINE_LEN 55000
#define INLINE static __inline
#ifdef _DEBUG
#define PRINT printf
#else
#define PRINT(...)
#endif
INLINE void write_file(const bool cover, const char * const buff, const char * const filename);
void print_time(const char *head)
{
struct timeb rawtime;
struct tm * timeinfo;
ftime(&rawtime);
timeinfo = localtime(&rawtime.time);
static int ms = rawtime.millitm;
static unsigned long s = rawtime.time;
int out_ms = rawtime.millitm - ms;
unsigned long out_s = rawtime.time - s;
ms = rawtime.millitm;
s = rawtime.time;
if (out_ms < 0)
{
out_ms += 1000;
out_s -= 1;
}
printf("%s date/time is: %s \tused time is %lu s %d ms.\n", head, asctime(timeinfo), out_s, out_ms);
}
int read_file(char ** const buff, const unsigned int spec, const char * const filename)
{
FILE *fp = fopen(filename, "r");
if (fp == NULL)
{
PRINT("Fail to open file %s, %s.\n", filename, strerror(errno));
return 0;
}
PRINT("Open file %s OK.\n", filename);
char line[MAX_LINE_LEN + 2];
unsigned int cnt = 0;
while ((cnt < spec) && !feof(fp))
{
line[0] = 0;
if (fgets(line, MAX_LINE_LEN + 2, fp) == NULL) continue;
if (line[0] == 0) continue;
buff[cnt] = (char *)malloc(MAX_LINE_LEN + 2);
strncpy(buff[cnt], line, MAX_LINE_LEN + 2 - 1);
buff[cnt][MAX_LINE_LEN + 1] = 0;
cnt++;
}
fclose(fp);
PRINT("There are %d lines in file %s.\n", cnt, filename);
return cnt;
}
void write_result(const char * const buff,const char * const filename)
{
// 以覆盖的方式写入
write_file(1, buff, filename);
}
void release_buff(char ** const buff, const int valid_item_num)
{
for (int i = 0; i < valid_item_num; i++)
free(buff[i]);
}
INLINE void write_file(const bool cover, const char * const buff, const char * const filename)
{
if (buff == NULL)
return;
const char *write_type = cover ? "w" : "a";//1:覆盖写文件,0:追加写文件
FILE *fp = fopen(filename, write_type);
if (fp == NULL)
{
PRINT("Fail to open file %s, %s.\n", filename, strerror(errno));
return;
}
PRINT("Open file %s OK.\n", filename);
fputs(buff, fp);
fputs("\n", fp);
fclose(fp);
}