mercredi 28 octobre 2015

C++ folder opening and file counting

So this is my code but I cant prevent it from printing out: . .. and it counts them as a file. I couldnt understand why. The output is: . 1files. .. 2files. course3.txt 3files. course2.txt 4files. course1.txt 5files.

But there are only 3 files... It should say 3 files instead it counts that . .. and i dont know its meaning.

int folderO(){
DIR *dir;
struct dirent *ent;
int nFiles=0;
if ((dir = opendir ("sampleFolder")) != NULL) {
  /* print all the files and directories within directory */
  while ((ent = readdir (dir)) != NULL) {
    std::cout << ent->d_name << std::endl;
    nFiles++;
    std::cout << nFiles << "files." << std::endl;
  }
  closedir (dir);
} 
else {
  /* could not open directory */
  perror ("");
  return EXIT_FAILURE;
}

}

Aucun commentaire:

Enregistrer un commentaire