Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am new here, so ask some simple questions. Thanks.

I want to read the names of all files in a directory one by one into a string or char []. I used readdir() from dirent.h. There are totally five files under that directory, but I read six files name (one of them is repeated and wrong). Please help me out and I post the code and output result.

code:
-----------------------------------------------------
C++
while((pent = readdir(path_dir))!= NULL)
{
//    if (pent-> d_type == DT_REG)
  {
  cout << pent->d_name << endl;
  }


//      if (pent->d_name[i] !='.'|| pent->d_name[i])

}

output:
-----------------------------------------------------
uk2.1327773900.data
uk1.1327881600.data
otbeta.1327670100.data
.use3.1327950000.data.kate-swp -> (wrong and repeated)
bj.1327521300.data
use3.1327950000.data
Posted
Updated 7-Feb-12 4:40am
v2
Comments
Richard MacCutchan 7-Feb-12 10:46am    
Can you provide an ls -al listing of this directory immediately followed by a run of your app and paste all the output (without changes) here?
Also the actual code that the app runs to get the file listing.
Chris Meech 7-Feb-12 10:47am    
You comment that a filename is repeated. But it appears to me rather that it is perhaps a special file of some sort as it starts with a '.' character. Is it possible that this is causing problems for the function readdir().
Chuck O'Toole 7-Feb-12 10:49am    
Yeah, what Chris said. Filenames that start with "." are special in Unix and may be supressed in an "ls" listing but will be seen by a programatic search of the directory.
Chuck O'Toole 7-Feb-12 10:50am    
Also, I don't know how you define "repeated" but the file you point to sure looks like a unique name to me. It may very well be "wrong" but it is definitely not "repeated"
Jochen Arndt 7-Feb-12 11:06am    
As already noted, file names beginning with a dot are hidden files in Unix. The appended '-swp' indicates that this may be some kind of temporary file that is actually opened by some application or has been left by a program that terminates abnormally.

1 solution

Along with everyone else,

It seems .use3.1327950000.data.kate-swp is a temporary file. Some editor such as vim use such temporary file the original file of the temporary file is use3.1327950000.data

If you open a file with some editor and then kill it without safely closing, you will see file like this. If you safely close the editor, then the editor will remove the swap file before it is closed.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900