From 65d881a78b604b89a8a41434ad5601cd789c6f46 Mon Sep 17 00:00:00 2001 From: Kayla Foroughi Date: Tue, 11 Mar 2025 14:23:36 -0400 Subject: [PATCH] reads the strings in file --- main.cpp | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/main.cpp b/main.cpp index 4ccc7e1..c65a0ca 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include #include +using namespace std; int main() { // Open file in binary input mode @@ -10,14 +11,28 @@ int main() { return 1; } + // Read number of strings 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 + // Process each 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 << buffer << std::endl; + + // Clean up + delete[] buffer; + } file.close(); return 0;