C ++:读取.txt内容并存储到2D数组中 [英] C++: Read .txt contents and store into 2D array

查看:84
本文介绍了C ++:读取.txt内容并存储到2D数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我和我的同伴突然之间手头有些紧要关头.我分配了一个导入功能,该功能可以读取如下所示的文本文件,并将其存储到2D数组中:

So me and my groupmates suddenly have some clutchwork at hand. I was assigned of making an import function that reads through a textfile that looks like this, and store it to a 2D array:

这些列用制表符分隔.突然出现这种情况时,我没有完整的项目文件,而且我离旧的可靠档案还很遥远,所以我尝试以最通用的方式来进行构想:

The columns are Tab-separated. As this suddenly came up, dont have the entire project file with me and I am nowhere near my old reliables, I tried conjuring this up in the most generic way possible:

void signal::import_data(string filename){
    ifstream file;

    file.open(filename.c_str());

    if(file.fail()){
        cerr << "file open fail" << endl;
    }else{
        while(!file.eof)
        {
            for(int j = 0; j < NumCols; j++){
                for(int i = 0; i < NumRows; i++){
                    file >> _data[j][i];
                }
            }
        }
    }
    file.close();
}

我这样做正确吗?我不确定这样的流媒体是否可以绕开选项卡,或者可以吗?

Am I doing this correct? Im not so sure if streaming like this can bypass tabs, or can it?

推荐答案

我认为这段代码:

while(!file.eof)
{
   for(int j = 0; j < NumCols; j++){
       for(int i = 0; i < NumRows; i++){
              file >> _data[j][i];
      }
   }
}

应替换为:

for(int j = 0; j < NumCols; j++)
{
   for(int i = 0; i < NumRows; i++)
   {
         if ( !(file >> _data[j][i]) ) 
         {
             std::cerr << "error while reading file";
             break;
         }
   }
   if ( !file ) break;
}

也就是说,如果您希望文件中存在NumCols * NumRows条目,为什么要显式检查文件结尾?让它读取,直到您读取NumCols * NumRows条目为止.读取后,它将自动退出循环.

That is, if you expect NumCols * NumRows entries to be there in the file, why explicitly check for end of file? Let it read till you read NumCols * NumRows entries is read. Once it read, it will automatically exit from the loop.

但是您必须在读取文件 NumCols * NumRows项之前检查文件是否以结尾,这就是我这样做的原因:

But you must check if the file ends before NumCols * NumRows entries is read, which is why I'm doing this:

if ( !(file >> _data[j][i]) ) 
{
    std::cerr << "error while reading file";
    break;
}

如果文件达到 eof 字符,或在完成读取NumCols * NumRows条目之前出现其他读取失败,则if中的条件将评估为true并打印错误消息并中断循环,它也会中断外部循环,因为表达式!file的计算结果为true.

If the file reaches eof character, OR some other read-failure before it finishes reading NumCols * NumRows entries, then the condition in the if would evaluate to true and it will print the error message and break the loop, it will break outer loop also, as the expression !file will evaluate to true.

有关如何使用C ++流读取文件的详细说明,请阅读以下主题的答案:

For a detail explanation as to HOW TO read files using C++ streams, read the answers from these topics:

  • What's preferred pattern for reading lines from a file in C++?
  • Why is iostream::eof inside a loop condition considered wrong?

这篇关于C ++:读取.txt内容并存储到2D数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆