gpt4 book ai didi

c++ - XCode 不会从文件中获取输入

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:37:20 24 4
gpt4 key购买 nike

出于某种原因,Xcode 不会从文件中获取输入,而 Visual C++ 会。当我在 xcode 中运行这个程序时,变量 numberRows 和 numberCols 保持为 0(它们在主函数中被初始化为 0)。当我在 Visual C++ 中运行它时,它们变成 30 和 30(maze.txt 的第一行是“30 30”,不带引号)。知道为什么会这样吗?

void readIn(int &numberRows, int &numberCols, char maze[][100]){

ifstream inData;
inData.open("maze.txt");

if (!inData.is_open()) {
cout << "Could not open file. Aborting...";
return;
}

inData >> numberRows >> numberCols;
cout << numberRows << numberCols;

inData.close();

return;

最佳答案

还有其他问题。
不幸的是,这很难说。

尝试刷新输出以确保您收到错误消息:

void readIn(int &numberRows, int &numberCols, char maze[][100])
{
ifstream inData("maze.txt");

if (!inData) // Check for all errors.
{
cerr << "Could not open file. Aborting..." << std::endl;
}
else
{
// Check that you got here.
cerr << "File open correctly:" << std::endl;

// inData >> numberRows >> numberCols;
// cout << numberRows << numberCols;

std::string word;
while(inData >> word)
{
std::cout << "GOT:(" << word << ")\n";
}

if (!inData) // Check for all errors.
{
cerr << "Something went wrong" << std::endl;
}
}
}

关于c++ - XCode 不会从文件中获取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1619643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com