From 0690c7b5c903cee21ce0d2277764e7121e6c836c Mon Sep 17 00:00:00 2001 From: Barzan Hayati Date: Sat, 28 Jun 2025 20:20:54 +0000 Subject: [PATCH] read text file --- CMakeLists.txt | 45 +++++++++++++++++++++++++++++++++++++++++++++ data/example.txt | 2 ++ src/main.cpp | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 data/example.txt create mode 100644 src/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..23ff4a4 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,45 @@ +cmake_minimum_required(VERSION 3.15) +project(TextReader LANGUAGES CXX) + +# Set C++ standard and enable modern practices +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) # Disable compiler-specific extensions + +# Configure where binaries will be placed +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + +# For larger projects +option(BUILD_TESTS "Build tests" OFF) +if(BUILD_TESTS) + enable_testing() + add_subdirectory(tests) +endif() + + +# Install target (for packaging) +# install(TARGETS text_reader DESTINATION bin) +# install(DIRECTORY data/ DESTINATION share/text_reader/data) + + +# Create the executable +add_executable(text_reader src/main.cpp) + +# Copy data files to build directory +file(COPY data/ DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/data) + +# Set compiler warnings +target_compile_options(text_reader PRIVATE + $<$:/W4 /WX> + $<$,$>:-Wall -Wextra -pedantic -Werror> +) + +# Platform-specific configurations +if(WIN32) + target_compile_definitions(text_reader PRIVATE _CRT_SECURE_NO_WARNINGS) +endif() + +# Include current directory for headers +target_include_directories(text_reader PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) \ No newline at end of file diff --git a/data/example.txt b/data/example.txt new file mode 100644 index 0000000..01ffec9 --- /dev/null +++ b/data/example.txt @@ -0,0 +1,2 @@ +/opt/nvidia/deepstream/deepstream-7.1/samples/streams/yoga.mp4 +/opt/nvidia/deepstream/deepstream-7.1/samples/streams/sample_720p.mp4 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..d491e2f --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +namespace fs = std::filesystem; + +int main() { + // Path handling works across platforms + fs::path data_dir = "/home/user2/temp_code/text_reader/data"; + fs::path file_path = data_dir / "example.txt"; + + try { + std::ifstream file(file_path); + if (!file.is_open()) { + throw std::runtime_error("Failed to open file: " + file_path.string()); + } + + std::cout << "Contents of '" << file_path << "':\n"; + std::cout << "-----------------\n"; + + std::string line; + int line_number = 1; + while (std::getline(file, line)) { + std::cout << "Line " << line_number++ << ": " << line << '\n'; + } + } + catch (const std::exception& e) { + std::cerr << "Error: " << e.what() << '\n'; + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} \ No newline at end of file