为什么我无法从文件中输入整数?

发布于 2024-08-25 19:03:57 字数 384 浏览 2 评论 0原文

我试图让这个 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 技术交流群。

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

发布评论

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

评论(5

放肆 2024-09-01 19:03:57

也许打开文件失败?
您可以使用fail()检查iffer的failbit是否设置

if(iffer.fail())
{
  cout << "Failed to open file." << endl;
}

Maybe opening the file failed?
You can check if the failbit of iffer is set with fail()

if(iffer.fail())
{
  cout << "Failed to open file." << endl;
}
垂暮老矣 2024-09-01 19:03:57

尝试过(VC9.0):

#include <iostream>
#include <fstream>

int main()
{
    using namespace std;

    int x = 0;
    cin >> x;

    ifstream iffer;
    int numbers[12];
    iffer.open("input.txt");
    for (int i = 0; i < 12; i++){
        iffer >> numbers[i];
    }
    for (int i = 1; i < 12; i++){
        numbers[i] = i;
    }

    return 0;
}

那有效,但是第二个循环是错误的。

Tried (VC9.0):

#include <iostream>
#include <fstream>

int main()
{
    using namespace std;

    int x = 0;
    cin >> x;

    ifstream iffer;
    int numbers[12];
    iffer.open("input.txt");
    for (int i = 0; i < 12; i++){
        iffer >> numbers[i];
    }
    for (int i = 1; i < 12; i++){
        numbers[i] = i;
    }

    return 0;
}

That worked, but the second loop is wrong.

它应该可以工作,但要注意,“get”总是只读取一个字符。这是我在 Mac OSX 上测试的,但这与您的操作系统无关,因为它是标准 C++:

#include <iostream>
#include <fstream>
using namespace std;

int main(){
    ifstream iffer;
    iffer.open("input.txt");
    char numbers[12];
    int i = 0;
    while (iffer.good()){
        numbers[i] =  iffer.get();
        i++;
    }
    for (int n = 0; n < 8; n++){
        cout << numbers[n];
    }        
    cout << endl;
    iffer.close();
}

我正在读取的文件“input.txt”。确保此文件在您的工作中目录!

12345678

读取文件时,每个字符都将存储在数组中。 文件

1 2 3 4 5 6 

所以当你有一个像你的数组将包含的

numbers[0] = '1'
numbers[1] = ' ' 
numbers[2] = '2'
numbers[3] = ' '
...

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++:

#include <iostream>
#include <fstream>
using namespace std;

int main(){
    ifstream iffer;
    iffer.open("input.txt");
    char numbers[12];
    int i = 0;
    while (iffer.good()){
        numbers[i] =  iffer.get();
        i++;
    }
    for (int n = 0; n < 8; n++){
        cout << numbers[n];
    }        
    cout << endl;
    iffer.close();
}

The file "input.txt" I am reading in. Make sure this file is in your working directory!:

12345678

While reading the file every character will be stored in your array. So when you have a file like

1 2 3 4 5 6 

your array will contain

numbers[0] = '1'
numbers[1] = ' ' 
numbers[2] = '2'
numbers[3] = ' '
...
つ低調成傷 2024-09-01 19:03:57

我在 Linux (g++ 3.4.4)Mac (g++ 4.0.1) 上尝试了您的代码,稍作修改,它工作得很好!

对于 Chuck,如果 input.txt 不存在,则 iffer.fail() 为 。既然你说情况并非如此...

另一种可能性是不同 input.txt 文件与你的文件不同预期的。如果它的数字太少,您会看到零(或其他垃圾值)。 (您可以使用 iffer.eof() 进行测试,尽管如果没有尾随空格(如换行符),则在读取最后一个数字后可能会(适当地)设置。所以在阅读之前测试 eof()!)

或者,您可能在代码中的其他地方有一个悬空指针,不恰当地破坏了某些内容。有时添加和删除大块代码将允许您手动“二分搜索”来查找此类问题的实际位置。

#include <iostream>
#include <fstream>

using namespace std;

#define SHOW(X) cout << # X " = \"" << (X) << "\"" << endl

int main()
{
  int x = 0;
  cin >> x;

  ifstream iffer;
  int numbers[12];
  iffer.open("input.txt");
  SHOW( iffer.fail() );
  SHOW( iffer.eof() );
  for (int i = 0; i < 12; ++i)
  {
    SHOW(i);
    SHOW(numbers[i]);
    iffer >> numbers[i];
    SHOW(numbers[i]) << endl;
  }
  for (int i = 0; i < 12; ++i)
    SHOW(numbers[i]);
  SHOW( iffer.fail() );
  SHOW( iffer.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.

#include <iostream>
#include <fstream>

using namespace std;

#define SHOW(X) cout << # X " = \"" << (X) << "\"" << endl

int main()
{
  int x = 0;
  cin >> x;

  ifstream iffer;
  int numbers[12];
  iffer.open("input.txt");
  SHOW( iffer.fail() );
  SHOW( iffer.eof() );
  for (int i = 0; i < 12; ++i)
  {
    SHOW(i);
    SHOW(numbers[i]);
    iffer >> numbers[i];
    SHOW(numbers[i]) << endl;
  }
  for (int i = 0; i < 12; ++i)
    SHOW(numbers[i]);
  SHOW( iffer.fail() );
  SHOW( iffer.eof() );
}
得不到的就毁灭 2024-09-01 19:03:57

这听起来像是路径问题。您在某处有一个“input.txt”文件,但不在当前目录中。如果这是在 GUI 应用程序中,请记住当前目录在某种程度上是不可预测的,您应该给出绝对路径或相对于某个已知路径的路径(例如当前应用程序 CFBundle 的路径)。

为了测试,我刚刚创建了一个 Unix 程序,其中包含包含在以下代码中的精确代码:

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    int x = 0;
    // your code
    cout << numbers[5] << endl;
    return 0;
}

它有效,因此如果这是在命令行程序中并且您从正确的目录启动它,则必须在某处更改当前目录在你的应用程序中。

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:

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    int x = 0;
    // your code
    cout << numbers[5] << endl;
    return 0;
}

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.

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