Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read files in a directory on Linux?

I am trying to read files in a given directory on Ubuntu 19.04. I am planning to use directory_iterator in standard library. I am using CLion IDE which uses compiler in the directory "usr/bin/c++". I guess this is g++ compiler and the version of g++ is 8.3 in my system. C++ version I am using is C++17.

I was able to run the code successfully on Visual Studio 2017 on Windows 10. But the code yielded segmentation fault error on Ubuntu while using CLion. The code below is from cppreference page about directory_iterator.

#include <fstream>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;

int main()
{
    fs::create_directories("sandbox/a/b");
    std::ofstream("sandbox/file1.txt");
    std::ofstream("sandbox/file2.txt");
    for(auto& p: fs::directory_iterator("sandbox"))
        std::cout << p.path() << '\n';
    fs::remove_all("sandbox");
}

I expected the code to print the files in the given folder but instead I got Segfault.

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV).

What could be the reason behind this issue? Could the problem be related to CMake?

like image 705
Aydin Özcan Avatar asked Apr 26 '19 08:04

Aydin Özcan


People also ask

How do I open a file or directory in Linux?

To open a directory on a computer with a graphical interface, you double-click on a folder. It opens, and you are now "in" that folder. To open a directory in a terminal, you use the cd command to change your current directory. This essentially opens that folder and places you in it.

How do I view a directory in Linux?

Linux or UNIX-like system use the ls command to list files and directories. However, ls does not have an option to list only directories. You can use combination of ls command, find command, and grep command to list directory names only. You can use the find command too.

How do I read a directory in terminal?

To see them in the terminal, you use the "ls" command, which is used to list files and directories. So, when I type "ls" and press "Enter" we see the same folders that we do in the Finder window.

How do I read a file in Linux terminal?

Getting started Crack open a terminal window and navigate to a directory containing one or more text files that you want to view. Then run the command less filename , where filename is the name of the file you want to view.


1 Answers

It looks like a bug in gcc 8.3

If you run it, it fail to link. May be in your environement the bug cause a segfault.

But if you update to gcc 9.1: it works fine.

Clang 9 is fine too, but all versions before fail to compile

Edit: As said by Galik if you add the flag -lstdc++fs it seems to work

Source : https://en.cppreference.com/w/cpp/filesystem

Notes

Using this library may require additional compiler/linker options. GNU implementation requires linking with -lstdc++fs and LLVM implementation requires linking with -lc++fs

like image 171
Martin Morterol Avatar answered Oct 02 '22 21:10

Martin Morterol