Website Summary Files Commits Branches @commit @diff @stats
path: root / Week06 / Exercise04.cpp
blob: 004dacc9772adb04ec24c60de940c65d7a94b8b5 (plain) (blame)
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
#include <iostream>
#include <fstream>

void printFileBetweenPositions(std::ifstream& inFile, int start, int end) {
	if (start >= end)
		return;

	int original = inFile.tellg();
	inFile.seekg(start, std::ios::beg);

	while (inFile.tellg() <= end) {
		std::cout.put(inFile.get());
	}

	inFile.seekg(original, std::ios::beg);
}

int main() {
	// Във файловите системи, имената на файлове са с ограничена дължина,
	// така че и ние да работим с ограничена дължина е коректно
	char fileName[1024];
	std::cin.getline(fileName, sizeof(fileName) / sizeof(*fileName));

	std::ifstream inFile(fileName);

	if (!inFile.is_open()) {
		std::cout << "Couldn't open file!" << std::endl;
	}
	else {
		int beg = inFile.tellg();

		// seekg(0, std::ios::end) ще ни заведе в края на файла,
		// след последния байт, все едно сме отишли при терминиращата
		// нула в низ. Затова трябва да отидем с една позиция назад,
		// за да стигнем последния (съществуващ) байт във файла.
		inFile.seekg(-1, std::ios::end);
		int endOfLine = inFile.tellg();

		while (inFile.tellg() != beg) {
			if (inFile.peek() == '\n') {
				printFileBetweenPositions(inFile, (int)inFile.tellg() + 1, endOfLine);
				endOfLine = inFile.tellg();
			}
			inFile.seekg(-1, std::ios::cur);
		}

		printFileBetweenPositions(inFile, inFile.tellg(), endOfLine);
		std::cout.flush();
	}

	return 0;
}