【问题标题】:How to read chars from a txt file to a 2d array C++如何将 txt 文件中的字符读取到二维数组 C++
【发布时间】:2013-10-18 21:32:44
【问题描述】:

所以我有一个包含球队和分数的文件,我需要将球队读入一个二维字符数组,然后将他们的分数读入一个整数数组。

这是我当前的函数代码,如何在团队名称完成后停止阅读,然后将分数存储在单独的数组中?

void getData (char array [][COLS], int rows, int scores [])
{
    ifstream inFile;     //Input file stream object

    //Open the file
    inFile.open ("scores.txt");

     if (!inFile)
    {
        cout << "Error opening data file!\n";
        exit(102);
    }

    for (int r = 0; r < rows; r++)
    {
        {
            //reads through columns
            for (int c = 0; c < COLS; c++)
            {
                inFile >> array[r][c];
            }
        }
    }

    for (int count = 0; count < ROWS; count++)
    {
        for(int i = 0; i < COLS; i++)
        {
            cout << array[count][i];
        }
    }

    inFile.close();
}

我的输入文件如下:

Jaquars 23
Colts 23
49ers 13
Lions 13
Titans 7
Redskins 38
Cardinals 14
Buccaneers 36
Seahawks 30
Lions 24
Bears 28
Packers 23
Bears 14
Rams 22
Texans 6
Packers 34

【问题讨论】:

  • 输入文件的布局是什么?如果它的形式是有一个团队并且每行得分,那么你最好使用 getline()。使用字符串向量也是一个好主意。让生活更轻松。
  • 输入文件行的一个示例是 Packers 21,然后在下一行 Browns 14 上,我必须使用 cstrings 进行此分配

标签: c++ arrays file char 2d


【解决方案1】:

这就是我的代码

void getData (char array [][COLS], int rows, int scores [])
{
     ifstream inFile;     //Input file stream object

    //Open the file
    inFile.open ("scores.txt");

     if (!inFile)
    {
        cout << "Error opening data file!\n";
        exit(102);
    }

for (int r = 0; r < rows; r++)
{
    int c = 0;
    char ch;
    // keep reading until we hit a space
    ch = inFile.get();
    while (ch != ' ')
    {
        array[r][c] = ch;
        c++;
        ch = inFile.get();
    }
    // now read the score
    inFile >> scores[r];
    // now read until we hit the trailing newline
    ch = inFile.get();
    while (ch != '\n' && ch != EOF)
    {
        ch = inFile.get();

    }
}

        for (int count = 0; count < rows; count++)
        {
            for(int i = 0; i < COLS; i++)
            {
                cout << array[count][i];
                for (int count2 = 0; count2 < rows; count2++)
                    cout << scores[count2];
            }
        }

    inFile.close();

    return;
}

【讨论】:

    【解决方案2】:

    可能是这样

    for (int r = 0; r < rows; r++)
    {
        int c = 0;
        // keep reading until we hit a space
        char ch = inFile.get();
        while (ch != ' ')
        {
            array[r][c] = ch;
            c++;
            ch = inFile.get();
        }
        // now read the score
        inFile >> scores[r];
        // now read until we hit the trailing newline
        ch = infile.get();
        while (ch != '\n')
        {
            ch = inFile.get();
        }
    }
    

    【讨论】:

    • Jaquars 23 Colts 23 49ers 13 Lions 13 Titans 7 Redskins 38 Cardinals 14 Buccaneers 36 Seahawks 30 Lions 24 Bears 28 Packers 23 Bears 14 Rams 22 Texas 6 Packers 34 这是我的档案,所有这些都在单独的行
    • 好吧,我上面的回答是完全错误的。我会将这些信息放入上面的问题中,以便每个人都能看到它(并尝试正确格式化)。
    • 你的问题的答案是循环直到你读到一个空格,当你读到一个空格时,你知道名字已经写完了,接下来你已经读到了分数。
    • 所以我需要在循环中使用 getline()?
    • 你不需要使用它。你可以,但这意味着重写你的代码。查看您编写的代码for (int c = 0; c &lt; COLS; c++),它总是准确地读取 COLS 字符。这显然是不正确的,因为您所有的团队名称都是不同的长度。我要说的是改变那个循环,这样它就不会总是读取相同数量的字符,它只会读取直到你到达一个空格。然后您就知道您已经阅读了团队名称(您的团队名称中都没有空格)。
    【解决方案3】:

    这应该让您知道如何去做。我没有编译它。但可以很好地说明这一点。 我强烈建议您使用基于地图的解决方案。或者使用向量。

    #include <string>
    #include <iostream>
    #include <fstream>
    #include<cstring>
    #include<cstdlib>
    using namespace std;
    
    int main() 
    {   
        ifstream in("DataFile.txt");
        string line;
        char * pch;
    
        while(getline(in, line))
        {
            pch = strtok (line.c_str()," ");
            while (pch != NULL)
            {
                //Put some logic here so that in first loop iteration
                //first segment(name) gets stored in your char arrays.
                //In second iteration, which will return the score, you store that
                //in the int array. Do not forget to use atoi() to convert to int before storing.
                pch = strtok (NULL," ");
            }
        }
    
        //There are more elegant ways to split a string in C++
        //http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c
        return 1;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-11
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      • 2021-11-23
      • 1970-01-01
      • 2022-01-16
      • 2014-04-06
      相关资源
      最近更新 更多