无法写入文件?
用COUT可以输出,但是写入文件却少了一部分
这个程序是获取指定目录的所有文件名,包括子目录下的文件,并把文件名放到temp.txt中
问题:只把目录下文件名写到了TXT中,但我用COUT输出时明明有子目录的文件?
#include <windows.h>
#include <iostream>
using namespace std;
int FindDir(char *szDir)
{
FILE* file2 = fopen("temp.txt", "w");
int i = 0;
//int number1 = number;
char directory[MAX_PATH];
char file[MAX_PATH];
HANDLE hFile;
WIN32_FIND_DATA fd;
memset( &fd, 0, sizeof(WIN32_FIND_DATA) );
strncpy(directory, szDir,MAX_PATH);
strcat(directory,"*.*");
hFile = FindFirstFile(directory, &fd);
do
{
if( fd.cFileName[0] != '.' )
{
if( fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
memset(file, 0, MAX_PATH);
strcpy(file, szDir );
strcat(file, fd.cFileName );
strcat(file, "\\" );
FindDir(file);
}
else
{
memset(file, 0, MAX_PATH);
strcpy(file, szDir );
strcat(file, fd.cFileName );
strcat(file, "\n" );
fwrite(file, sizeof(char), strlen(file), file2);
cout<<file <<endl;
}
}
}while( FindNextFile( hFile, &fd) );
fclose(file2);
return 0;
}
int main()
{
char path[MAX_PATH];
cin>>path;
/*输入例:c:\\dfdfsdf\\ */
//int number = strlen(path);
FindDir(path);
return 0;
}