別の基準を確認する前に何か処理が必要な場合は、入力を確認する最も良い方法は何ですか? コードスニペット:cinはすでに処理されていますか?
#include <string>
#include <iostream>
#include <dirent.h>
bool has_suffix(const std::string &str, const std::string &suffix);
void get_path_get_exit(std::string &path_input);
int main()
{
DIR *dir;
struct dirent *ent;
std::string path_input;
std::getline(std::cin, path_input);
const char *path = path_input.c_str();
dir = opendir(path);
check:
while ((dir) == NULL && !path_input.empty()){
/* could not open directory */
std::cout << "Whoops, that didn't work. Please enter a valid directory path." << std::endl << "-->";
std::getline(std::cin, path_input);
path = path_input.c_str();
closedir(dir);
dir = opendir(path);
}
if ((dir) != NULL) {
unsigned int counter = 0;
while ((ent = readdir (dir)) != NULL) {
counter++;
}
/*check if the folder is empty*/
if (counter == 0){
/*how to surpass the goto statement?*/
goto check;
}
std::string files[counter];
closedir(dir);
dir = opendir(path);
counter = 0;
while ((ent = readdir (dir)) != NULL) {
files[counter] = ent->d_name;
std::cout << "[" << counter+1 << "] " << files[counter] << std::endl;
counter++;
}
closedir(dir);
}
}
あなたが見ることができるように、私は開かれたディレクトリが空である場合、私は、チェックしようとしている文の場合はCIN入力し、次の中をチェックしようとしています。もう一度whileループに入り、再び上の基準で新しい入力をチェックするために、トップ 'チェック'の先頭に "ホップ"するより良い方法はありますか? 後藤は確かに動作しますが、私はそれを少し恥ずかしく感じます。
はwhile条件では '!path_input.empty()'であってはなりませんか? –
もちろん、私はそれを間違ってコピーして申し訳ありません。私はそれを修正しました! – Dominik