C++ 我想读取文件并将目的地写入目的地数组,将价格写入价格数组

C++ 我想读取文件并将目的地写入目的地数组,将价格写入价格数组,c++,arrays,C++,Arrays,我已经写了一个包含4个度假目的地和价格的文件,所以我想读取该文件并将目的地写入目的地数组,将价格写入价格数组。文本格式如下所示 suncity 250 ushakamarue 300 krugerPark 450 Tablemountain 340 这就是我所拥有的 #include<iostream> #include<fstream> #include<iomanip> #include<string> using namespace std

我已经写了一个包含4个度假目的地和价格的文件,所以我想读取该文件并将目的地写入目的地数组,将价格写入价格数组。文本格式如下所示

suncity 250
ushakamarue 300
krugerPark 450
Tablemountain 340
这就是我所拥有的

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;

//define a constant for the number of lines to read 
#define NUM_READ_LINES 4
int main()
{

    // array of line numbers each line being less or equal to 100 chars

     char destination[NUM_READ_LINES][100];
     //string price[30];
     int counter = 0;
     //ofstream outfile;
     ofstream outfile("program.txt");
     if(outfile.is_open())
     {
         outfile <<"suncity 250\n ";
         outfile <<" ushakamarue 300\n";
         outfile <<" krugerPark 450\n";
         outfile <<" Tablemountain 340\n";
     }

     else 
       cout <<"Unable to open to file";

     outfile.close();

     //open a file
       ifstream infile;
       infile.open("program.txt");


      if(infile.good())
      {
        //Read throuh file and load into array
    while(!infile.eof() && (counter < NUM_READ_LINES))
    {
        infile.getline(destination[counter], 100);
        counter++;
    }
    //loop hrough the array which we just put together
    for (int i=0; i < counter;i++ )
        {
            cout << destination[i]<<endl;
        }
    }
    infile.close();


    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
//为要读取的行数定义一个常量
#定义NUM_READ_行4
int main()
{
//行号数组,每行小于或等于100个字符
字符目的地[NUM_READ_line][100];
//串价[30];
int计数器=0;
//出流孔的直径;
出流文件(“program.txt”);
if(outfile.is_open())
{
outfile类似这样的内容:

     string destination[NUM_READ_LINES];
     string price[NUM_READ_LINES];

     ifstream infile("program.txt");
     for (int ii = 0; ii < NUM_READ_LINES; ++ii) {
         infile >> destination[ii] >> price[ii];
     }
字符串目的地[NUM_READ_line];
字符串价格[NUM_READ_line];
ifstream-infle(“program.txt”);
对于(int ii=0;ii>目的地[ii]>>价格[ii];
}

您有什么问题吗?目前程序正在将整行suncity 250读取为目的地,而不是仅将suncity读取为目的地[1],将250读取为价格[1]。请帮助。谢谢,它成功了。