폴더의 모든 파일의 이름을 바꾸는 기능이 있지만 특정 파일의 이름을 다시 지정합니다. http://i.imgur.com/JjN8Qb2.png, 같은 종류의 "오류"가 매 10 번째마다 계속 발생합니다 번호. 이 "오류"의 원인은 무엇입니까?readdir() : 특정 파일 다시 읽기
함수의 두 인수는 폴더의 경로와 첫 번째 파일의 시작 값을 지정합니다.
int lookup(std::string path, int *start){
int number_of_chars;
std::string old_s, file_format, new_s;
std::stringstream out;
DIR *dir;
struct dirent *ent;
dir = opendir (path.c_str());
if (dir != NULL) {
// Read pass "." and ".."
ent = readdir(dir);
ent = readdir(dir);
// Change name of all the files in the folder
while((ent = readdir (dir)) != NULL){
// Old string value
old_s = path;
old_s.append(ent->d_name);
// Get the format of the image
file_format = ent->d_name;
number_of_chars = file_format.rfind(".");
file_format.erase(0,number_of_chars);
// New string value
new_s = path;
out << *start;
new_s += out.str();
new_s.append(file_format);
std::cout << "Successfully changed name on " << ent->d_name << "\tto:\t" << *start << file_format << std::endl;
// Switch name on the file from old string to new string
rename(old_s.c_str(), new_s.c_str());
out.str("");
*start = *start+1;
}
closedir (dir);
}
// Couldn't open
else{
std::cerr << "\nCouldn't open folder, check admin privileges and/or provided file path\n" << std::endl;
return 1;
}
return 0;
}
당신이 바꾸고있는 바로 그 것을 열거하고 있습니다. 디렉토리 내용 이미 변경 한 파일 이름 (새 이름)의'std :: set <>'을 만들고 그 세트의 어떤 것도 건너 뜁니다. 그 중 하나를 * 전체 * 디렉토리 * 첫 번째 * 읽기, 모든 파일 이름을'std :: list <>'또는 다른 적당한 컨테이너에 로딩 한 다음, 그리스트를 반복하면서 길을 따라 이름을 바꾼다. – WhozCraig
SideNote : 파일에 * 첫 번째 *가 항상 "."인 보장이있는 경우 그리고 "..", 나는 그것을 모르고있다. 내가 틀렸다면 (* 처음이 아닌) 명시 적으로 * 테스트해야합니다. – WhozCraig
좋아요, 그런 느낌이었습니다.하지만 일단 openir을 호출하면 폴더에서 내가 한 것을 무시한다는 것을 알았습니다. 그리고 몇 번이나 dirent (windows 전용)를 사용하여 항상 두 개의 상위 폴더가있는 것처럼 보였으므로이를 변경하여 읽기 만하는 대신 테스트를 수행합니다. 도와 주셔서 감사합니다! –