From e5f8dfba8c44682ffc21a3a6e3496576a03b255e Mon Sep 17 00:00:00 2001 From: cf2031293 Date: Tue, 11 Mar 2025 14:22:07 -0400 Subject: [PATCH 1/2] Add binary file reading functionality --- main.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 4ccc7e1..32060b1 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,8 @@ #include #include - +#include +#include +using namespace std; int main() { // Open file in binary input mode std::fstream file("tips.shp", std::ios_base::binary | std::ios_base::in); @@ -13,11 +15,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; From 0a42e871fda30390109d958ab9f0c8168879843d Mon Sep 17 00:00:00 2001 From: cf2031293 Date: Tue, 11 Mar 2025 14:24:27 -0400 Subject: [PATCH 2/2] Add binary file reading functionality --- main.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index 32060b1..723868c 100644 --- a/main.cpp +++ b/main.cpp @@ -1,8 +1,7 @@ #include #include -#include -#include using namespace std; + int main() { // Open file in binary input mode std::fstream file("tips.shp", std::ios_base::binary | std::ios_base::in);