如何从输入文件C ++中读取多行数据变量?

时间:2012-12-05 01:34:31

标签: c++ string file-io

我正在研究一个从输入文件中读取道路数据的程序,如下所示:

INTERSECTIONS:
1   0.5 Speedway and Campbell    // intersection ID, safety rating, name
2   0.3 Park and Grant
3   0.1 Euclid and Grant

STREETS:
1   2   3            // intersection 1, intersection 2, distance in between
2   3   1
3   1   2

表格中的每个值都以制表符(\ t)字符

分隔

我必须将这些数据读入图表指定的相应变量中。 我主要是在寻找一个简单的答案:是否可以使用getline一次读取一行,然后将行分成每一位数据?如果是这样,我该怎么做?

以下是我目前在主文件中的内容:

#include "graph.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

vector <Vertex*> vertices;
vector <Edge*> edges;

int main (int argc, char *argv[]) {

    if( argc != 4 )
    {
        std::cout << "\nUsage: " << argv[0] << " distanceInMiles startingIntersection streetMapGraphFile \n\n" << endl;
        return -1;
    }

    ifstream mapfile ("streetMapGraphFile");

    int count = 0;
    int loc = 0;

    Graph streetMap;

    if (mapfile.is_open())
    {
        while ( mapfile.good() )
        {
            while ( count != 1 ) {
                string line;
                getline(mapfile,line);
                if ( line == "INTERSECTIONS:" )
                {
                    getline(mapfile,line);
                }
                else if ( line == "" )
                {
                    count++;
                    break;  //  to avoid reading blank line line
                }
                stringstream ss(line);
                ss >> streetMap.intersection->streetID >> streetMap.intersection->safetyIndex;
                getline(ss, streetMap.intersection->name);
            }
            string line2;
            getline(mapfile,line2);
            if ( line2 == "STREETS:" )
            {
                getline(mapfile,line2);
            }
            //  TODO: Read in the edges/streets here
            stringstream ss2(line2);
            ss2 >> streetMap.street->intersection1 >> streetMap.street->intersection2 >> streetMap.street->distance;
        }
        mapfile.close();
    }
    else { cerr << "Error: unable to open file" << endl; }

    return 0;
}

我已经更改了我的代码以在行字符串上实现字符串流,但是当我逐步调试时,我的编译器在执行此行后崩溃了:

ss >> streetMap.intersection->streetID >> streetMap.intersection->safetyIndex;

错误读取“safejogger.exe中0x0F592208(msvcp110d.dll)的未处理异常:0xC0000005:访问冲突写入位置0xCCCCCCCC。”

1 个答案:

答案 0 :(得分:1)

是的,这是可能的。一种方法是使用字符串流: http://www.cplusplus.com/reference/sstream/stringstream/

示例:

#include <sstream>
#include <iostream>
#include <string>

using namespace std;

template <typename T>
void getVal(stringstream &ss, T &val)
{
  string token;
  getVal(ss, token);
  stringstream ss2(token);
  ss2>>val;
}

template <>
void getVal<string>(stringstream &ss, string &val)
{
  getline(ss, val, '\t'); //Note the separator specification                                                                                                                                                                                                                                                                                                              
}

int main(int argc, char** argv)
{
  string line = "1\t2\t3";
  stringstream ss(line);
  int intersection1, intersection2, distance;
  getVal(ss, intersection1);
  getVal(ss, intersection2);
  getVal(ss, distance);
  cout<<"Distance was: "<<distance<<endl;

  string line2 = "1\t0.5\t Speedway and Campbell";
  stringstream ss2(line2);
  int intersectionID;
  float safetyRating;
  string intersectionName;
  getVal(ss2, intersectionID);
  getVal(ss2, safetyRating);
  getVal(ss2, intersectionName);
  cout<<"Intersection name: ["<<intersectionName<<"]"<<endl;

  return 0;
}