简体   繁体   English

将数据存储在 memory 以供以后访问

[英]Store Data in memory to access later on

New to C++. C++ 的新手。 I have a code that spins thru directories to look for a specific file (123.txt) and lists the result in a textbox.我有一个代码可以通过目录旋转以查找特定文件(123.txt)并在文本框中列出结果。 What i need to do is store these result in memory so i can access it later.我需要做的是将这些结果存储在 memory 中,以便我以后可以访问它。 An array maybe?也许是一个数组? I'm not exactly sure how that's done.我不确定那是怎么做的。

Here's the code to execute it:这是执行它的代码:

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

Here's the code to search for 123.txt and list the restult in a logfile.这是搜索 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);
    }

I thought maybe i could add this to the code, but it doesn't work我想也许我可以将它添加到代码中,但它不起作用

string listOfDir[20]

std::string(dir) >> listOfDir;

The standard C++ language does not have overloads of operator>> to input arrays.标准 C++ 语言没有operator>>重载来输入 arrays。
The following does not work:以下不起作用:

std::cin >> listOfDir;

The C++ language does not have any functions to split a string. C++ 语言没有任何拆分字符串的功能。
The following does not work:以下不起作用:

std::string(dir) >> listOfDir;

The compiler can't find any overload of operator>> that takes a std::string parameter and an array parameter.编译器找不到任何带有std::string参数和数组参数的operator>>重载。 The statement would be equivalent of:该声明相当于:

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

In summary, you'll need to write the code yourself to parse or split a string;总之,您需要自己编写代码来解析或拆分字符串; or you can search the internet for a string library.或者您可以在互联网上搜索字符串库。

If you use std::vector , this code may be more useful:如果您使用std::vector ,此代码可能更有用:

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

The above code fragment appends a copy of dir to the vector listOfDir .上面的代码片段将dir的副本附加到向量listOfDir

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM