-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_exercise.cpp
39 lines (35 loc) · 1.02 KB
/
file_exercise.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
#include <iostream>
#include <fstream>
using namespace std;
int main() {
//zapis
const unsigned int LINE_LENGTH = 80;
typedef char Line[LINE_LENGTH];
Line line;
// ofstream bin ("file.bin", ios::binary);
// if (bin.is_open()) {
// while (cin.getline(line,LINE_LENGTH)) bin.write(line,LINE_LENGTH);
// bin.close();
// }
// else {
// cerr << "Soubor se nepodařilo otevřít" << endl;
// }
unsigned int lineNum;
ifstream read ("file.bin", ios::binary);
if (read.is_open()){
read.seekg(0,std::ifstream::end);
int size = read.tellg();
while (cin >> lineNum){
if ((lineNum-1)*LINE_LENGTH<size){
read.seekg((lineNum-1)*LINE_LENGTH);
read.read(line,LINE_LENGTH);
cout << lineNum << " - " << line << endl;
}
else cerr << "Větší řádek než co to má!!!" << endl;
}
}
else {
cerr << "Soubor se nepodařilo otevřít" << endl;
}
return 0;
}