如何将文本文件读入二维数组

时间:2013-11-30 14:37:23

标签: c++ arrays file

嘿伙计我是新手将txt文件读取到数组,所以我需要将这个txt文件temperature.txt读取为二维数组。这是我需要阅读的文件,我尝试编写的代码符合但我不确定它是否正确读取

 T(F)       R1        R2       R3        R4
 95.0     95.20     66.10     43.10     29.00
 96.0     96.10     67.60     43.50     31.20
 97.0     97.40     67.00     43.70     30.50
 98.0     97.20     69.10     44.10     30.70
 99.0     98.90     68.00     44.70     32.80
 100.0    99.50     71.10     45.10     31.50
 101.0   101.00     71.20     45.30     31.60
 102.0   101.60     71.00     45.70     30.50
 103.0   101.80     73.10     46.30     32.50
 104.0   103.70     73.50     46.60     32.70
 105.0   105.60     72.80     47.10     33.60

更新我再次这样做而没有看你的答案,但这会有效吗?

using namespace std;
#include<fstream>
#include<iostream>
#include<cmath>
#include<iomanip>
#include<stdlib.h>

int main ()
{

     char temp[11] [5];
    ifstream tempin ("c:\\mydoc2\\temperaturedata.txt");
    tempin>>temp[0]>>temp[1]>>temp[2]>>temp[3]>>temp[4]>>temp[5]>>temp[6]>>temp[7]>>temp[8]  


          >>temp[9]>>temp[10];
    while(!tempin.fail())
    {


       cout<< temp[0] << "  " << temp[1] << "  " << temp[2] << "  " << temp[3]<< "  " << temp[4]<< "  " << temp[5] << "  " << temp [6] << 
        "  " << temp [7] << "  " << temp[8] << "  " << temp[9] << "  " << temp[10];
        tempin>>temp[0]>>temp[1]>>temp[2]>>temp[3]>>temp[4]>>temp[5]>>temp[6]>>temp[7]  

    >>temp[8]>>temp[9]>>temp[10];
    }
    cout << endl << endl;
    system("pause");
     return 0;
        }

5 个答案:

答案 0 :(得分:1)

你有一个string myArray[5];,但你正在阅读55个元素 - 这应该不起作用!

答案 1 :(得分:1)

您的代码中存在许多错误。

1)你已经编写了一个read函数,但是你从不调用它,因此它永远不会执行。

2)您的代码中没有2D数组。 2D数组看起来像这个double myArray[10][10];。 (这是一个10乘10的2D二维阵列。)

3)您正在尝试读取浮点数,但您的数组是一个字符串数组。

4)你的数组大小为5,但是你试着读55个项目。

5)打开文件后,你有一个无限循环,只打印出“No file \ n”。不确定为什么要在循环中打印出错误消息。通常,您只需打印一次错误消息。

我可以继续,但我认为主要的一点是你是初学者,而且你现在无法在不引入错误的情况下编写三行代码(抱歉是苛刻但基于以上内容)是真的)。所以重要的一课是,你不应该尝试一次编写三行以上的代码。

尝试这样的事情

a)写一些打开文件的代码,测试它并检查它是否打开文件

b)添加一些代码给a)读取一个号码,测试它并检查它是否读取了一个号码。

c)将读取的一个数字代码替换为读取一维数字数组的代码。测试并检查它是否有效。

等。等

重点是逐渐建立你想要的程序,并在你去的时候测试每个阶段。我无法强调这是多么重要。每个专业程序员都是这样工作的,但因为你是初学者,你必须采取的步骤比经验丰富的程序员小得多。

答案 2 :(得分:0)

尝试这样的事情:

#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
using namespace std;

int main() {
    string line;
    ifstream myfile ("C:/temps.txt");

    int dim_x = 12;
    int dim_y = 5;
    double temps[dim_x][dim_y] ;

    //init the array if neccesary
    for(int x = 0;x<dim_x;x++)
        for(int y = 0; y<dim_y;y++)
            temps[x][y]=0;

    if (myfile.is_open()){
        for ( int x=0; getline (myfile,line); x++ ){
            int y=0;
            istringstream iss(line);
            while(iss){
                string sub;
                iss >> sub;
                temps[x][y] = ::atof(sub.c_str());
                y++;
            }
        }
        myfile.close();
      }

    cout << "TEMPS:" << endl;
    for(int x = 0;x<dim_x;x++){
        for(int y = 0; y<dim_y;y++)
            cout<<temps[x][y]<<"  ";
        cout<<endl;
    }

    return 0;
}

答案 3 :(得分:0)

如果您的文件内容是这样的:

 95.0     95.20     66.10     43.10     29.00
 96.0     96.10     67.60     43.50     31.20
 97.0     97.40     67.00     43.70     30.50
 98.0     97.20     69.10     44.10     30.70
 99.0     98.90     68.00     44.70     32.80
 100.0    99.50     71.10     45.10     31.50
 101.0   101.00     71.20     45.30     31.60
 102.0   101.60     71.00     45.70     30.50
 103.0   101.80     73.10     46.30     32.50
 104.0   103.70     73.50     46.60     32.70
 105.0   105.60     72.80     47.10     33.60

然后,您很容易获得这些值。但是代码中存在一些错误:

  1. while(!indata.fail()):此行将导致死循环。请改用if(!indata)
  2. 您需要一个类型为double的二维数组,而不是string
  3. 以下是可行的:

    #include<fstream>
    #include<iostream>
    #include<cmath>
    #include<iomanip>
    #include<stdlib.h>
    using namespace std;
    void read ();
    int main ()
     {
        read();
        system("PAUSE");
        return 0;
     }
    
    void read()
    {
        ifstream indata("E:\\temperature.txt"); //that's my path,you should use yours.
        if(!indata)
        {
            cout<<"No file."<< endl;
        }
        if(indata.is_open())
        {
            double myArray[11][5];
            for(int i = 0; i < 11; ++i)
            {
                for(int j = 0; j < 5; ++j)
                {
                    indata >> myArray[i][j];
                    //cout<<myArray[i][j]<<"\t";
                }
                //cout<<endl;
            }
        }
    }
    

    txt文件不包含第一行时,这可能有效:T(F) R1 R2 R3 R4,如果此行退出,则应使用getline()将文件指针移动到第二行,你可以在哪里阅读数据。

答案 4 :(得分:0)

我做了一些改进

#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

void read ();

int main ()
{
    read();
    return 0;   
}

void read()
{
    ifstream indata(".\\temperaturedata.txt");
    if(indata == NULL)
    {
        cout<< "Opening file failed."<< endl;
        return;
    }
    const int columns = 5;
    const int rows = 11;

    double myArray[rows][columns];
    string tmp;
    getline(indata, tmp);
    for(int i = 0; i < rows; ++i)
    {
        for (int j = 0; j < columns; ++j)
            {
                cout << "\t" << flush;
                indata >> myArray[i][j];
                cout.setf(ios::fixed);
                cout << setprecision(2) << myArray[i][j] <<flush;
            }
            cout << endl;
    }
    indata.close();
}
相关问题