質問

私はC ++に新しいですし、私はディレクトリエントリを操作するためにdirent.hヘッダーを試しています。あなたの後に以下の小さなアプリケーションのコンパイルが、pukesは、ディレクトリ名をしなやか。誰かが私にヒントを与えることはできますか? int型は、whileループを提供することがあり終了します。私は私の問題を特定しようとする試みでループを取り除いています。

ありがとう!

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

using namespace std;

int main()
{

char *dirname = 0;
    DIR *pd = 0;
    struct dirent *pdirent = 0;

    int quit = 1;



    cout<< "Enter a directory path to open (leave blank to quit):\n";
    cin >> dirname;

    if(dirname == NULL)
    {
        quit = 0;

    }
        pd = opendir(dirname);

    if(pd == NULL)
    {
        cout << "ERROR: Please provide a valid directory path.\n";
    }


    return 0;
}
役に立ちましたか?

解決

あなたがC ++を使用している場合は、CHAR *または配列を使用しない、使用のstd ::文字列ます:

#include <string>
....   
string dirname;
cout<< "Enter a directory path to open (leave blank to quit):\n";
getline( cin, dirname );
if ( dirname == "" ) {
   exit(1);
}
....   
pd = opendir(dirname.c_str() );

他のヒント

変更:

char *dirname = 0;

char dirname[PATH_MAX] = "";
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top