-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfread.cpp
More file actions
83 lines (77 loc) · 1.79 KB
/
fread.cpp
File metadata and controls
83 lines (77 loc) · 1.79 KB
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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <sys/stat.h>
/*
getFileSize
Function that is used to get the file size length of the specified file
*/
size_t getFileSize(const char* fileName) {
struct stat st;
if (stat(fileName, &st) != 0) {
return 0;
}
return st.st_size;
}
int main(int argc, char* argv[])
{
bool isWatching;
isWatching = false;
if (argc < 3) {
/*
Options:
-s - File stream reading (supported)
-t - Reads the last line of the file. The tail end.
*/
std::cout << "Usage is -<option> <file>\n-s - File streaming\n-t - Only read the tail\n\n";
exit(0);
}
else {
std::ifstream inFile;
char* myFile = argv[2];
std::cout << argv[0] << "\n";
if (std::string(argv[1]) == "-s")
isWatching = true;
inFile.open(myFile, std::ios_base::binary); //windows files require a binary read to prevent duplicate line breaks \r\n
if (!inFile) {
std::cerr << "Unable to open: " << myFile << "\n";
exit(1);
}
else {
int length = 0, oldLength = 0;
do {
length = getFileSize(myFile);
if (length != oldLength && length > 0) {
bool hasReadNewLine = false;
for (int i = length - 2; i > 0; i--) {
inFile.seekg(i);
char c = inFile.get();
if (c == '\r' || c == '\n') {
if (hasReadNewLine) {
oldLength = i;
break;
}
else {
hasReadNewLine = true;
}
}
}
char * s = new char[length - oldLength];
inFile.get(s, length - oldLength);
if (inFile)
std::cout << s << "\n";
else{
std::cout << "Unable to read " << oldLength << " to " << length;
length = 0;
}
oldLength = length;
delete[] s;
}
_sleep(1);
} while (length && isWatching);
inFile.close();
}
}
return 0;
}