CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2010
    Posts
    3

    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!!!!!
    Attached Files Attached Files

  2. #2
    Join Date
    Dec 2010
    Posts
    3

    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;
    }

  3. #3
    Join Date
    May 2008
    Posts
    38

    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/

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured