-
1 Attachment(s)
Load & Saving Files
Hi!!
I have one problem with my code. I don't know how to make input and output file for my version of "Dir" program. Input file contains directory for search (C:\ATI) for example and output file contains results of program. Here is my code. Thanks!!!!!
-
Re: Load & Saving Files
There is my code
#include <iostream>
#include <windows.h>
#include <cstdio>
using namespace std;
void RecursiveFileSearch(char *folder_name)
{
WIN32_FIND_DATA file_find_data;
HANDLE file_find_handle;
cout<< folder_name << endl;
char *find_string = new char[strlen(folder_name)+5];
strcpy(find_string, folder_name);
strcat(find_string, "\\*.*");
//если find_string и file_find_data не равны, возвращается INVALID_HANDLE_VALUE
file_find_handle = FindFirstFile(find_string, &file_find_data);
if (file_find_handle != INVALID_HANDLE_VALUE)
{
do
{
if (file_find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if ((strcmp(file_find_data.cFileName, ".") == 0) ||
(strcmp(file_find_data.cFileName, "..") == 0))
{
continue;
}
char *find_subdir = new char[strlen(folder_name)+strlen(file_find_data.cFileName)+5];
strcpy(find_subdir, folder_name);
strcat(find_subdir, "\\");
strcat(find_subdir, file_find_data.cFileName);
RecursiveFileSearch(find_subdir);
delete[] find_subdir;
}
else
{
cout << file_find_data.cFileName << endl;
}
}
/// continue searching
while (FindNextFile(file_find_handle, &file_find_data));
}
delete[] find_string;
}
int main(int argc, char *argv[])
{
RecursiveFileSearch(argv[1]);
system("PAUSE");
return 0;
}
-
Re: Load & Saving Files
Why don't you just redirect the output on the command line by calling
Code:
myprog.exe dirname > results.txt
If you really want it done in code then there's plenty of either OS dependant ways or other methods using the standard library. Check out fstream here ... http://www.cplusplus.com/reference/i...ostream/write/
-
Re: Load & Saving Files
This is more like dir /S
The code seems pretty alright but since you use strcpy you really should make find_subdir a null-string after allocating (*find_subdir = 0).
To make an output open a file if there's an argv[2] argument an put your output to that file.