0

I'm writing code in C++ and I need to read a file, problem is my file structure is a bit complex and I just cannot figure out how to use my ifstream to read it. I think I tried all possible combinations... but it just doesn't work, I guess I'm doing something wrong but I can't figure it out.

Here is a minimal reproduction of my problem.

structure :

.
├── build
├── CMakeLists.txt
└── src
    ├── file
    │   └── test.txt
    ├── load
    │   └── loadfile.hpp
    └── main.cpp

CMakeLists.txt :

cmake_minimum_required(VERSION 3.10)
project(BaseProject)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "-O3 -g -std=c++17 -Wall -Wextra -pedantic")

file(GLOB SRC
        "src/*.h"
        "src/*.hpp"
        "src/load/*.h"
        "src/load/*.hpp"
        "src/load/*.cpp"
        )

add_executable(exec ${SRC} src/main.cpp)

main.cpp

#include "load/loadfile.hpp"

int main(){
    load();
    return 0;
}

test.txt : (not very relevent but meh)

test

loadfile.hpp

#include <fstream>
#include <string>
#include <iostream>

void loadFile(const std::string& file){
    std::ifstream i(file, std::ifstream::in);
    std::string str;
    i >> str;
    std::cerr << str;
    i.close();
}

void load(){
    loadFile("../file/test.txt");
}

output is empty and program finishes normally.

Yorokobii
  • 17
  • 6
  • 5
    Now might be a good time to learn about the concept of *current working directory*. When you run a program, its process will have a *current working directory*. If you run from a console or terminal then it's usually the terminals current directory. Relative paths (paths not beginning with a `/`) are always relative to the current working directory. You need to make sure that the relative path in the program is valid for the programs current working directory when running. – Some programmer dude Feb 26 '20 at 10:05
  • 5
    Also, *always* check for failure when you do something that can fail, like for example opening a file. – Some programmer dude Feb 26 '20 at 10:10
  • oh... so it's from the build directory if I run the program from here... ok got it ! I thought it was relative to a file or something. Thanks for the help ! – Yorokobii Feb 26 '20 at 10:31
  • you could make copying the text.txt to build part of the build process. Later when you install it you will also have to put files in the right place – 463035818_is_not_an_ai Feb 26 '20 at 10:38
  • https://stackoverflow.com/questions/17337602/how-to-get-error-message-when-ifstream-open-fails – rustyx Feb 26 '20 at 11:11

2 Answers2

0

This might help, when you call install your files will be copied to destination specified.

cmake_minimum_required(VERSION 3.10)
project(BaseProject)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "-O3 -g -std=c++17 -Wall -Wextra -pedantic")
set(OUTPUT_DIR "Yours to fill, working dir")

file(GLOB RESOURCES "./file/test.txt")

file(GLOB SRC
        "src/*.h"
        "src/*.hpp"
        "src/load/*.h"
        "src/load/*.hpp"
        "src/load/*.cpp"
        )

add_executable(exec ${SRC} src/main.cpp)

install (FILES ${RESOURCES} DESTINATION ${OUTPUT_DIR})
0

Answer from Some programmer dude in the comments:

Now might be a good time to learn about the concept of current working directory. When you run a program, its process will have a current working directory. If you run from a console or terminal then it's usually the terminals current directory. Relative paths (paths not beginning with a /) are always relative to the current working directory. You need to make sure that the relative path in the program is valid for the programs current working directory when running.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Yorokobii
  • 17
  • 6