From 49c8277db4fe95e32df930e334bf2560f4867293 Mon Sep 17 00:00:00 2001 From: ishaan-78 Date: Tue, 11 Mar 2025 14:22:31 -0400 Subject: [PATCH] Finished reading file --- main.cpp | 15 ++++++++++++++- cmake-build-debug/tips.shp => tips.shp | Bin 2 files changed, 14 insertions(+), 1 deletion(-) rename cmake-build-debug/tips.shp => tips.shp (100%) diff --git a/main.cpp b/main.cpp index 4ccc7e1..92b7ac7 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include #include +using namespace std; int main() { // Open file in binary input mode @@ -12,13 +13,25 @@ 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)); + cout << num_strings; // 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; + } file.close(); return 0; } diff --git a/cmake-build-debug/tips.shp b/tips.shp similarity index 100% rename from cmake-build-debug/tips.shp rename to tips.shp