|
-
May 25th, 2001, 09:59 PM
#1
Thread Starter
Hyperactive Member
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
Matt 
-
May 26th, 2001, 09:33 AM
#2
Member
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
-
May 26th, 2001, 12:32 PM
#3
Thread Starter
Hyperactive Member
The if statement checks to see weather its a directory. If so then call the function with that directory.
Matt 
-
May 26th, 2001, 12:57 PM
#4
Thread Starter
Hyperactive Member
Matt 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|