-
writing files to txtfile
Code:
#define MAX_PATH 256;
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream.h>
#include <fstream.h>
void dir(char *path);
int main()
{
dir("\\");
return 0;
}
void dir(char *path)
{
FILE *fp;
char dirname[MAX_PATH];
HANDLE fhwnd;
WIN32_FIND_DATA WFD;
SetCurrentDirectory(path);
fhwnd = FindFirstFile("*.*", &WFD);
fp = fopen("C:/Matt/files2.doc","w");
while(FindNextFile(fhwnd,&WFD))
{
if(
(WFD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (strcmp(WFD.cFileName, "..") && strcmp(WFD.cFileName, "."))
)
{
GetCurrentDirectory(MAX_PATH,dirname);
if(strncmp(&dirname[strlen(dirname) - 1], "\\", 1)){
strcat(dirname, "\\");
}
strcat(dirname, WFD.cFileName);
cout << dirname;
fprintf(fp,dirname);
fprintf(fp, "\n");
cout << endl;
dir(dirname);
}
}
FindClose(fhwnd);
fclose(fp);
}
I cannot see all the results when this finds all the files in the console window. So I made it write it to a file but it only writes a couple on the file path's. Why is it only printing a few?? Thanks
-
Here is the problem
This line is goofing the code up:
This line calls the dir function again (calls itself) and so it reopens the file that you are writing to again.
Code:
fp = fopen("C:/Matt/files2.doc","w");
Does that help?
chilibean
-
The if statement checks to see weather its a directory. If so then call the function with that directory.
-