【问题标题】:Store Data in memory to access later on将数据存储在内存中以供以后访问
【发布时间】:2020-09-21 02:46:22
【问题描述】:

C++ 新手。我有一个代码通过目录旋转以查找特定文件(123.txt)并在文本框中列出结果。我需要做的是将这些结果存储在内存中,以便我以后可以访问它。也许是一个数组?我不确定这是怎么做到的。

这是执行它的代码:

outfile1.open("pxutil1.log");

    DWORD dwSize = MAX_PATH;
    char szLogicalDrives[MAX_PATH];
    DWORD dwResult = GetLogicalDriveStrings(dwSize, szLogicalDrives);
    if (dwResult == 0)
    {
        // error handling...
    }
    else if (dwResult > MAX_PATH)
    {
        // not enough buffer space...
    }
    else
    {
        for (char* szSingleDrive = szLogicalDrives; *szSingleDrive != 0; szSingleDrive += (lstrlenA(szSingleDrive) + 1))
        {
            if (GetDriveTypeA(szSingleDrive) == DRIVE_FIXED)
                FindFile(szSingleDrive);
        }
    }

这是搜索 123.txt 并在日志文件中列出结果的代码。

std::string dir = directory;
    if ((!dir.empty()) && (dir.back() != '\\') && (dir.back() != '/'))
        dir += '\\';

    WIN32_FIND_DATAA file;
    HANDLE search_handle = FindFirstFileA((dir + "*").c_str(), &file);
    if (search_handle == INVALID_HANDLE_VALUE)
    {
        if (GetLastError() != ERROR_FILE_NOT_FOUND)
        {
            // error handling...
            //::MessageBox(NULL, "File not found", "", MB_OK);
        }
    }
    else
    {
        do
        {
            if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                if ((lstrcmpA(file.cFileName, ".") != 0) && (lstrcmpA(file.cFileName, "..") != 0))
                {
                    //FindFile(dir + file.cFileName);
                    if (!ExcludeDir(dir + file.cFileName))
                    {
                        FindFile(dir + file.cFileName);
                    }
                }
            }
            else
            {
                if (lstrcmpA(file.cFileName, "123.txt") == 0)
                {                   
                    outfile1 << dir.c_str() << endl; //write to log file                
                }
            }
        } while (FindNextFileA(search_handle, &file));

        if (GetLastError() != ERROR_NO_MORE_FILES)
        {
            ::MessageBox(NULL, "No more files", "", MB_OK);
        }

        FindClose(search_handle);
    }

我想也许我可以将它添加到代码中,但它不起作用

string listOfDir[20]

std::string(dir) >> listOfDir;

【问题讨论】:

标签: c++ file winapi directory


【解决方案1】:

标准 C++ 语言没有将 operator&gt;&gt; 重载到输入数组。
以下方法不起作用:

std::cin >> listOfDir;

C++ 语言没有任何拆分字符串的函数。
以下不起作用:

std::string(dir) >> listOfDir;

编译器找不到任何采用std::string 参数和数组参数的operator&gt;&gt; 重载。该语句相当于:

operator>>(std::string, std::string[]);

总之,您需要自己编写代码来解析或拆分字符串;或者您可以在互联网上搜索字符串库。

如果你使用std::vector,这段代码可能更有用:

std::vector<std::string> listOfDir;  
//...
listOfDir.push_back(dir);

上述代码片段将dir 的副本附加到向量listOfDir

【讨论】:

    猜你喜欢
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多