将令牌存储到数组中

发布于 2025-01-05 06:06:47 字数 744 浏览 0 评论 0原文

作为 C++ 的新手,我正在尝试创建一个统计程序来练习编码。我希望获得一个文本文件,读取它并将值存储到可以执行数学运算的数组中。我被困

 main ()
 {
      char output[100];
      char *charptr;
      int age[100];
      ifstream inFile;
      inFile.open("data.txt");
      if(!inFile)
      {
            cout<<"didn't work";
            cin.get();
            exit (1);
      }

      inFile.getline(output,100);
      charptr = strtok(output," ");
      for (int x=0;x<105;x++)
      {
           age[x] = atoi(charptr);
           cout<<*age<<endl;

      }

     cin.get();
}

在上面的代码中,我试图将主题年龄存储到 int 数组“age”中,将年龄保留在文件的第一行。我打算如上所述使用 strtok,但我无法将标记转换为数组。

正如你所看到的,我是一个十足的菜鸟,请耐心等待,因为我正在自学。 :)

谢谢

PS:我已经阅读了类似的线程,但无法遵循那里给出的详细代码。

A novice at C++, i am trying to create a stats program to practice coding. i am hoping to get a text file, read it and store values into arrays on which i can perform mathematical operations. i am stuck here

 main ()
 {
      char output[100];
      char *charptr;
      int age[100];
      ifstream inFile;
      inFile.open("data.txt");
      if(!inFile)
      {
            cout<<"didn't work";
            cin.get();
            exit (1);
      }

      inFile.getline(output,100);
      charptr = strtok(output," ");
      for (int x=0;x<105;x++)
      {
           age[x] = atoi(charptr);
           cout<<*age<<endl;

      }

     cin.get();
}

in the code above, I am trying to store subject ages into the int array 'age', keeping ages in the first line of the file. I intend to use strtok as mentioned, but i am unable to convert the tokens into the array.

As you can obviously see, I am a complete noob please bear with me as I am learning this on my own. :)

Thanks

P.S: I have read similar threads but am unable to follow the detailed code given there.

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

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

发布评论

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

评论(1

累赘 2025-01-12 06:06:47

for 循环存在一些问题:

  • 由于 age 有 100 个元素,但 for 中存在终止条件,因此可能会越界> 循环是 x < 105
  • 使用前不检查 charptr 是否为 NULL
  • for 循环内不后续调用 strtok()
  • 打印 age 元素不正确

以下是 for 循环的修复示例:

charptr = strtok(output, " ");
int x = 0;
while (charptr && x < sizeof(age)/sizeof(age[0]))
{
    age[x] = atoi(charptr);
    cout << age[x] << endl;
    charptr = strtok(NULL, " ");
    x++;
}

由于这是 C++,建议:

  • 使用 std::vector 而不是固定大小的数组
  • 使用std::getline() 以避免指定固定大小的缓冲区来读取行,
  • 使用 std::copy()istream_iterator 来解析整数行

例如:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>

int main ()
{
    std::vector<int> ages;
    std::ifstream inFile;
    inFile.open("data.txt");
    if(!inFile)
    {
        std::cout<<"didn't work";
        std::cin.get();
        exit (1);
    }

    std::string line;
    std::getline(inFile, line);

    std::istringstream in(line);

    std::copy(std::istream_iterator<int>(in),
              std::istream_iterator<int>(),
              std::back_inserter(ages));

    return 0;
}

There are a few issues with the for loop:

  • Possibility of going out-of-bounds due to age having 100 elements, but terminating condition in for loop is x < 105
  • No check on charptr being NULL prior to use
  • No subsequent call to strtok() inside for loop
  • Printing of age elements is incorrect

The following would be example fix of the for loop:

charptr = strtok(output, " ");
int x = 0;
while (charptr && x < sizeof(age)/sizeof(age[0]))
{
    age[x] = atoi(charptr);
    cout << age[x] << endl;
    charptr = strtok(NULL, " ");
    x++;
}

As this is C++, suggest:

  • using std::vector<int> instead of a fixed size array
  • use the std::getline() to avoid specifying a fixed size buffer for reading a line
  • use std::copy() with istream_iterator for parsing the line of integers

For example:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>

int main ()
{
    std::vector<int> ages;
    std::ifstream inFile;
    inFile.open("data.txt");
    if(!inFile)
    {
        std::cout<<"didn't work";
        std::cin.get();
        exit (1);
    }

    std::string line;
    std::getline(inFile, line);

    std::istringstream in(line);

    std::copy(std::istream_iterator<int>(in),
              std::istream_iterator<int>(),
              std::back_inserter(ages));

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