C++运行时参数发生变化
我有一个非常简单的函数,它应该读取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我敢打赌,您从返回指向局部变量的指针的函数中获取文件名,例如:
以上内容无效 -
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:
The above isn't valid -
Filename
contents are on stack, and will be invalidated whengetFilename()
returns.vector<string> fileLines;
will be located in the same memory, and therefore the datafileName
points to will change when you step over thevector
constructor.EDIT: See e.g. C++ compiler warning - returning local variable for more information on this