forked from StrongKs/binaryIO_WarmUp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
49 lines (39 loc) · 1.25 KB
/
main.cpp
File metadata and controls
49 lines (39 loc) · 1.25 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
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Open file in binary input mode
std::fstream file("tips.shp", std::ios_base::binary | std::ios_base::in);
int sentence_count = 0;
file.read(reinterpret_cast<char*>(&sentence_count), sizeof(int));
//cout << sentence_count << endl;
if (!file.is_open()) {
std::cout << "Error opening file" << std::endl;
return 1;
}
unsigned int num_strings;
// TODO: Read number of strings
// Hint: file.read((char *)&num_strings, sizeof(num_strings));
vector<string> sentences;
for (int i = 0; i < sentence_count; i++) {
int string_length = 0;
file.read(reinterpret_cast<char*>(&string_length), sizeof(int));
string sentence;
for (int j = 0; j < string_length; j++) {
char curr_char;
file.read(reinterpret_cast<char*>(&curr_char), sizeof(char));
sentence.push_back(curr_char);
}
sentences.push_back(sentence);
}
// TODO: Add loop to:
// 1. Read string length
// 2. Read string characters
// 3. Print string
file.close();
for (int i = 0; i < sentence_count; i++) {
cout << sentences[i] << endl;
}
return 0;
}