为什么我无法从文件中输入整数?
我试图让这个 C++ 代码从文本文件中输入一系列数字:
int x = 0;
cin >> x;
ifstream iffer;
int numbers[12];
iffer.open("input.txt");
for (int i = 0; i < 12; i++){
iffer >> numbers[i];
}
这似乎不适用于 Mac。 无论文本文件中的值如何,每个单元格都等于 0。换句话说,ifstream 没有分配数字。
我怎样才能做到这一点?这是 Mac 问题吗?如果是,我该如何让它工作?
谢谢!
安东尼·格利亚琴科
I'm trying to get this C++ code to input a series of numbers from a text file:
int x = 0;
cin >> x;
ifstream iffer;
int numbers[12];
iffer.open("input.txt");
for (int i = 0; i < 12; i++){
iffer >> numbers[i];
}
This doesn't seem to work on the Mac.
Every cell will equal to 0 regardless of the values in the text file. In other words, the ifstream isn't assigning the numbers.
How can I make this work? Is it a Mac issue and if so, how can I get it to work?
Thanks!
Anthony Glyadchenko
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
也许打开文件失败?
您可以使用fail()检查iffer的failbit是否设置
Maybe opening the file failed?
You can check if the failbit of iffer is set with fail()
尝试过(VC9.0):
那有效,但是第二个循环是错误的。
Tried (VC9.0):
That worked, but the second loop is wrong.
它应该可以工作,但要注意,“get”总是只读取一个字符。这是我在 Mac OSX 上测试的,但这与您的操作系统无关,因为它是标准 C++:
我正在读取的文件“input.txt”。确保此文件在您的工作中目录!:
读取文件时,每个字符都将存储在数组中。 文件
所以当你有一个像你的数组将包含的
It should work, but be aware, that "get" will always just read one character. This is what I tested, also on Mac OSX, but this has nothing to do with your OS, since it is standard C++:
The file "input.txt" I am reading in. Make sure this file is in your working directory!:
While reading the file every character will be stored in your array. So when you have a file like
your array will contain
我在 Linux (g++ 3.4.4) 和 Mac (g++ 4.0.1) 上尝试了您的代码,稍作修改,它工作得很好!
对于 Chuck,如果 input.txt 不存在,则 iffer.fail() 为 。既然你说情况并非如此...
另一种可能性是不同 input.txt 文件与你的文件不同预期的。如果它的数字太少,您会看到零(或其他垃圾值)。 (您可以使用 iffer.eof() 进行测试,尽管如果没有尾随空格(如换行符),则在读取最后一个数字后可能会(适当地)设置。所以在阅读之前测试 eof()!)
或者,您可能在代码中的其他地方有一个悬空指针,不恰当地破坏了某些内容。有时添加和删除大块代码将允许您手动“二分搜索”来查找此类问题的实际位置。
I tried your code, slightly modified, on both Linux (g++ 3.4.4) and Mac (g++ 4.0.1) and it works just fine!
With respect to Chuck, if input.txt does not exist, iffer.fail() is true. Since you say that's not the case...
Another possibility is a different input.txt file than what you expected. If it had too few numbers, you'd see zeros (or other garbage values). (You could test with iffer.eof(), though that might be set (appropriately) after reading the last number if there's no trailing whitespace (like a newline). So test eof() before reading!)
Alternatively, you could have a dangling pointer elsewhere in your code trashing something inappropriately. Sometimes adding and removing large chunks of code will permit you to manually "binary search" for where such problems actually lie.
这听起来像是路径问题。您在某处有一个“input.txt”文件,但不在当前目录中。如果这是在 GUI 应用程序中,请记住当前目录在某种程度上是不可预测的,您应该给出绝对路径或相对于某个已知路径的路径(例如当前应用程序 CFBundle 的路径)。
为了测试,我刚刚创建了一个 Unix 程序,其中包含包含在以下代码中的精确代码:
它有效,因此如果这是在命令行程序中并且您从正确的目录启动它,则必须在某处更改当前目录在你的应用程序中。
This sounds like a path issue. You have an "input.txt" file somewhere, but not in the current directory. If this is in a GUI application, keep in mind that the current directory is somewhat unpredictable and you should either give an absolute path or a path relative to some known path (e.g. the path to the current application CFBundle).
Just to test, I just created a Unix program including your precise code wrapped in the following code:
It worked, so if this is in a command-line program and you launched it from the right directory, you must be changing the current directory somewhere in your app.