C++运行时参数发生变化

发布于 2024-10-31 22:05:53 字数 1818 浏览 0 评论 0原文

我有一个非常简单的函数,它应该读取 txt 文件并按字符串类型的一个向量返回所有行。我已经使用调试器多次执行过它,我唯一注意到的是“fileName”的值在 ifstream 声明行上发生了变化。我做错了什么?谢谢。

vector<string> readFile(char* fileName)
{
    vector<string> fileLines;
    fileLines.clear();
    string line;
    ifstream myfile (fileName);
    if (myfile.is_open())
    {
        while ( myfile.good() )
        {
            getline (myfile,line);
            fileLines.push_back(line);
        }
        myfile.close();
    }
    return fileLines;
}

//////////////////////////////////////////

我有这个功能,该功能应返回当前文件中所有TXT文件的文件名目录。

   vector<char*> getFileList()
    {
        vector<char*> fileNames;
        fileNames.clear();
        WIN32_FIND_DATA FindFileData;
        HANDLE hFind = INVALID_HANDLE_VALUE;
        char currentPath[_MAX_PATH];
        getCurrentPath(currentPath);
        strncat(currentPath, "\\*", 3);
        if (hFind = FindFirstFile(currentPath, &FindFileData))
        {

            string fileExtension = getExt(FindFileData.cFileName);
            if (fileExtension == "txt" || fileExtension == "TXT")
            {
                fileNames.push_back(FindFileData.cFileName);
            }
            while(FindNextFile(hFind, &FindFileData) != 0)
            {
                string fileExtension = getExt(FindFileData.cFileName);
                if (fileExtension == "txt" || fileExtension == "TXT")
                    fileNames.push_back(FindFileData.cFileName);
            }
        }
        return fileNames;
    }

这就是该函数的调用方式:

vector<char*> inputFileList = getFileList();
if (inputFileList.size() > 0)
{
    for (int a=0; a<inputFileList.size(); a++)
    {
        fileLines = readFile(inputFileList[a]);
    }
}

I've a very simple function that should read a txt file and return all lines by one vector of the type string. I've stepped trough it quite a few times with the debugger and the only thing i've noticed is that the value of "fileName" changes on the line of the ifstream declaration. What am i doing wrong?? Thx.

vector<string> readFile(char* fileName)
{
    vector<string> fileLines;
    fileLines.clear();
    string line;
    ifstream myfile (fileName);
    if (myfile.is_open())
    {
        while ( myfile.good() )
        {
            getline (myfile,line);
            fileLines.push_back(line);
        }
        myfile.close();
    }
    return fileLines;
}

//////////////////////////////////

I have this function that should return the filenames of all txt files in the current directory.

   vector<char*> getFileList()
    {
        vector<char*> fileNames;
        fileNames.clear();
        WIN32_FIND_DATA FindFileData;
        HANDLE hFind = INVALID_HANDLE_VALUE;
        char currentPath[_MAX_PATH];
        getCurrentPath(currentPath);
        strncat(currentPath, "\\*", 3);
        if (hFind = FindFirstFile(currentPath, &FindFileData))
        {

            string fileExtension = getExt(FindFileData.cFileName);
            if (fileExtension == "txt" || fileExtension == "TXT")
            {
                fileNames.push_back(FindFileData.cFileName);
            }
            while(FindNextFile(hFind, &FindFileData) != 0)
            {
                string fileExtension = getExt(FindFileData.cFileName);
                if (fileExtension == "txt" || fileExtension == "TXT")
                    fileNames.push_back(FindFileData.cFileName);
            }
        }
        return fileNames;
    }

That's how the function is going to be called:

vector<char*> inputFileList = getFileList();
if (inputFileList.size() > 0)
{
    for (int a=0; a<inputFileList.size(); a++)
    {
        fileLines = readFile(inputFileList[a]);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

§对你不离不弃 2024-11-07 22:05:53

我敢打赌,您从返回指向局部变量的指针的函数中获取文件名,例如:

char * getFilename() {
 char Filename[100] = "/foo";
 return Filename;
} 

以上内容无效 - Filename 内容在堆栈上,并将失效当 getFilename() 返回时。 向量<字符串> fileLines; 将位于同一内存中,因此当您单步执行 vector 构造函数时,fileName 指向的数据将会发生变化。

编辑:有关详细信息,请参阅 C++ 编译器警告 - 返回局部变量

My bet would be that you get your filename from a function which returns a pointer to a local variable, e.g. something like this:

char * getFilename() {
 char Filename[100] = "/foo";
 return Filename;
} 

The above isn't valid - Filename contents are on stack, and will be invalidated when getFilename() returns. vector<string> fileLines; will be located in the same memory, and therefore the data fileName points to will change when you step over the vector constructor.

EDIT: See e.g. C++ compiler warning - returning local variable for more information on this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文