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
45 lines (35 loc) · 1.22 KB
/
main.cpp
File metadata and controls
45 lines (35 loc) · 1.22 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
#include <fstream>
#include <iostream>
using namespace std;
int main() {
// Open file in binary input mode
std::fstream file("tips.shp", std::ios_base::binary | std::ios_base::in);
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));
file.read((char *)&num_strings, sizeof(num_strings));
std::cout << "Number of strings: " << num_strings << std::endl;
// TODO: Add loop to:
// 1. Read string length
// 2. Read string characters
// 3. Print string
for (unsigned int i = 0; i < num_strings; i++) {
unsigned int str_len;
// Read length of string (including null terminator)
file.read((char *)&str_len, sizeof(str_len));
// Create buffer and read string
char* buffer = new char[str_len];
file.read(buffer, str_len);
// Print string (stop at null terminator)
std::cout << "String " << (i + 1) << " (length " << (str_len - 1)
<< "): " << buffer << std::endl;
// Clean up
delete[] buffer;
}
file.close();
return 0;
}