read text file

This commit is contained in:
Barzan Hayati 2025-06-28 20:20:54 +00:00
parent 4e11b12b63
commit 0690c7b5c9
3 changed files with 81 additions and 0 deletions

45
CMakeLists.txt Normal file
View File

@ -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
$<$<CXX_COMPILER_ID:MSVC>:/W4 /WX>
$<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>:-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})

2
data/example.txt Normal file
View File

@ -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

34
src/main.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <iostream>
#include <fstream>
#include <string>
#include <filesystem>
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;
}