1

my path is not detected when I'm trying to input a space in the directory.

Here is my code

#include<iostream> 
#include <dirent.h >

using namespace std;

void directory_contents(char *directory_path)
{
    DIR * dh;
    struct dirent * contents;

    dh = opendir(directory_path);

    if (!dh)
    {
        cout << "The given directory is not found";
        return;
    }
    while ((contents = readdir(dh)) != NULL)
    {
        string name = contents->d_name;
        cout << name << endl;
    }
    closedir(dh);
}
int main()
{
    int x, y;
    char txt[1000];
    cout << "Enter path:";
    cin >> txt;

    cout << "Text is " << txt;
    directory_contents((char*) txt);
    return 0;
}   

enter image description here

I was thinking to use trim but I suppose there something I need to add to my code.I hope you can help me thanks!

Simon Kraemer
  • 5,700
  • 1
  • 19
  • 49
  • You will need to use `getline` and then parse the path-with-spaces from the resulting string – AndyG Jun 22 '21 at 12:17
  • Welcome to Stack Overflow. Please visit our [help center](https://stackoverflow.com/help), with special attention to the page on [minimal complete examples](https://stackoverflow.com/help/minimal-reproducible-example). You can simplify your code by hard-coding an example string that reproduces the error. And in this case, it appears that if you had *printed out* the variable before trying to use it, you would have seen that it was not what you intended. – Beta Jun 22 '21 at 12:20
  • Hi Sir, can you give me an example regarding this one? thank you! – Don Don Mendez Jun 22 '21 at 12:35
  • 1
    Here: [https://stackoverflow.com/a/7119907/487892](https://stackoverflow.com/a/7119907/487892) – drescherjm Jun 22 '21 at 13:18

0 Answers0