【问题标题】:C++ read file transfer to 2d ArrayC ++读取文件传输到二维数组
【发布时间】:2016-04-07 01:37:49
【问题描述】:

所以正如标题所说,我正在尝试从文件中读取并将字符保存到带有空格的二维数组中,但它卡在了 do while 循环和文件的最后一个字符中。对于如何解决这个问题,有任何的建议吗?我考虑过使用 eofbit 但我想不通然后我尝试通过 || 退出 do while 循环它与 eof 没有工作。它似乎只是挂在文件的最后一个字母上。提前感谢您的建议

char ch[100][100];
int row_count =0;
int col_count = 0;
int col[100];
char temp; 
bool exit = false;
ifstream infile("data.txt");
if( infile.is_open())
{
    while(!infile.eof())
    {
        do
        {
            infile.get(temp);
            char[row_count][col_count] = temp;
            col_count++;

        }while(temp != '\n' || !infile.eof());
        col[row_count] = col_count;
        row_count++;
        col_count= 0;
    }
}

for(int i = 0; i <= 2; i++)
{
    for(int j=0; i <= col[i]; j++)
    {
        cout << ch[i][j];
    }
    cout << endl;
}

return 0;

}

【问题讨论】:

  • 不要使用 while (!infile.eof()) 参见此处:stackoverflow.com/questions/5605125/… 尝试使用 while(infile.get(temp)) 代替。另外为什么两个嵌套循环具有相似的条件?只需添加一个 if 语句
  • 使用流的getline 方法会简单得多
  • 请给最有帮助的答案打分。

标签: c++ arrays file-io


【解决方案1】:

循环是因为temp != '\n',在文件末尾这个条件总是为真,所以第二个条件永远不会被验证,因为你用或检查它。如果您想保留您的代码,请首先将!infile.eof() 放在首位。

但是,如果您想要一种最佳方式和一种简单的方式来读取文件。你应该使用getline

std::string line;
while(std::getline (infile,line))
{
    //do something for every char in the string...
}

【讨论】:

    【解决方案2】:

    我没有编译这个,但这应该可以工作: 阅读 cmets 并从中学习。

    #include <iostream> // for cout
    #include <fstream> // for infile     
    
    int main()
    {
        const int RowSize = 100, ColSize = 100;
        char ch[RowSize][ColSize];
        int row_count = 0;
        int col_count = 0;
        int col[ColSize];
        char temp;
        bool exit = false;
    
        std::ifstream infile;
    
        infile.open("data.txt"); // open the file
    
        if (!infile.is_open()) // check if opened succseffuly
        {
            std::cout << "File didn't load successfully!" << std::endl; // prompt the user
            getchar(); // wait for any input
            return 0; // terminate
        }
    
        while (infile.get(temp)) // while reading succesffuly
        {
            // if you are here, means you have valid data
    
            if (temp == '\n') // check if end of the line
            {
                col_count = 0; // reset cols
                row_count++; // increment rows
                ch[RowSize][ColSize] = NULL; // lock char array
                continue; // continue the loop iteration
            }
            else
            {
                ch[row_count][col_count] = temp; // store temp in your array
            }
            col_count++; // increment cols on every read
        }
    
        for (int i = 0; i <= 2; i++) // this will display junk if your line is less than 100 characters (check NULL if you don't want that)
        {
            for (int j = 0; i <= col[i]; j++)
            {
                std::cout << ch[i][j];
            }
            std::cout << std::endl;
        }
    
        return 0;
    }
    

    【讨论】:

    • 感谢您的回复并抽出宝贵时间。它没有编译,但我能够对其进行调试以使其适合我。更重要的是,它帮助我理解了我的错误,并提醒我我没有使用的正确技术。谢谢你。 @FirstStep
    • @DevilGraceland 很高兴它有帮助:) 你能说明为什么它没有编译吗?如果它回答了您的问题,请务必将其标记为答案
    【解决方案3】:

    iostream::eof 只会在读取流的末尾后返回 true。它并不表示下一次读取将是流的结尾。参考this。因此,修改后的代码将如下面的代码 sn-p 所示。

    char ch[100][100];
    int row_count =0;
    int col_count = 0;
    int col[100];
    char temp; 
    bool exit = false;
    ifstream infile("data.txt");
    if( infile.is_open())
    {
        while(!(infile>>temp).eof())
        {
    
            ch[row_count][col_count] = temp;
            col_count++;
    
            if(temp == '\n'){
                col[row_count] = col_count;
                row_count++;
                col_count= 0;
            }
        }
    }
    
    for(int i = 0; i <= 2; i++)
    {
        for(int j=0; i <= col[i]; j++)
        {
            cout << ch[i][j];
        }
        cout << endl;
    }
    
    return 0;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 2016-08-11
      • 1970-01-01
      • 2015-03-19
      • 2014-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多