将递归输出保存到文本文件

时间:2017-11-30 19:34:35

标签: c++ winapi recursion

我试图以递归方式列出目录中的文件。但是,当我将输出保存到文本文件时,它可以工作,但文件内容仍然被重置。因此文件大小为2Kb,然后重置为1Kb,30Kb重置为1Kb,依此类推。代码没有将所有输出保存到文本文件中,只有一些最后一行保存到output.txt。

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <Psapi.h>
#include <fstream>

using namespace std;

void listDir(wchar_t * szCurrentDirectory);

int main()
{
    // return current directory where app run
    DWORD nBufferLength = MAX_PATH;
    wchar_t szCurrentDirectory[MAX_PATH];
    GetCurrentDirectory(nBufferLength, szCurrentDirectory);

    listDir(szCurrentDirectory);
    system("pause");
    return 0;
}

void listDir(wchar_t * szCurrentDirectory)
{
    wchar_t addPath[MAX_PATH] = L"\\*";
    WIN32_FIND_DATA FindFileData;
    wchar_t buf[MAX_PATH];
    swprintf(buf, MAX_PATH,L"%s%s", szCurrentDirectory, addPath);

    HANDLE hFind;
    hFind = FindFirstFile(buf, &FindFileData);
    wofstream myfile("c:/output.txt");

    if (myfile.is_open())
    {
        if (hFind != INVALID_HANDLE_VALUE)
        {
            do
            {
                // Check if is a folder or not.
                if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_REPARSE_POINT)
                {
                    // Ignore current folder and parent folder.
                    if (wcscmp(FindFileData.cFileName, L".") && wcscmp(FindFileData.cFileName, L"..") != 0)
                    {
                        if (wcscmp(FindFileData.cFileName, L"$RECYCLE.BIN") != 0)
                        {
                            // return current directory where app run
                            wchar_t filepath[10000];
                            // Append slash to current directory.
                            swprintf(filepath, 10000, L"%s%s%s", szCurrentDirectory, L"\\", FindFileData.cFileName);
                            // Output the file.
                            myfile << filepath << endl;

                            if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                            {
                                // Recursive 
                                listDir(filepath);
                            } // if directory
                        } // if not recycle bin
                    } // if not . or ..
                } if not reparse point
            } while (FindNextFile(hFind, &FindFileData) != 0);

            FindClose(hFind);
            myfile.close();
        }
    }
}

尝试在c:drive中运行应用程序并查看output.txt中的输出。输出保持重置。

3 个答案:

答案 0 :(得分:2)

将打开的文件作为参数传递。

更改

void listDir(wchar_t * szCurrentDirectory) 

void listDir(wofstream & myfile,wchar_t * szCurrentDirectory)

然后从listDir()删除文件开头代码。

删除此行:

wofstream myfile("c:/output.txt");

然后在main()中调用它:

wofstream myfile("c:/output.txt");
listDir(myfile,szCurrentDirectory);

最后在listDir()更改

listDir(filepath);

listDir(myfile,filepath);

答案 1 :(得分:1)

正确答案是肯定打开文件一次(在主程序中),然后将流传递到listdir函数。

然而 ......还有另一种选择。在这种情况下,它可能是类似的情况。您可以在追加模式下打开文件,因此内容永远不会被覆盖。您只需将打开文件的行更改为:

std::wofstream myfile("c:/output.txt", std::ios_base::app);

输出将写入文件末尾,保留现有内容。

答案 2 :(得分:0)

在main中打开文件,并将句柄作为第二个参数传递给listDir函数。

不要在listDir中打开它

相关问题