Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <fstream>
#include <iostream>
using namespace std;

int main() {
// Open file in binary input mode
Expand All @@ -13,11 +14,31 @@ int main() {
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;
Expand Down