简体   繁体   English

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

[英]How do I read multiple data variables from line from input file C++?

I'm working on a program that reads in road data from an input file like this: 我正在开发一个从输入文件中读取道路数据的程序,如下所示:

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

Each value in the table is separated by a tab (\\t) character 表中的每个值都由制表符(\\ t)分隔

I have to read this data into the appropriate variables specified by a graph. 我必须将这些数据读入图形指定的适当变量中。 I'm mostly looking for a simple answer: is it possible to use getline to read one line at a time and then separate the line into each bit of data? 我主要是在寻找一个简单的答案:是否可以使用getline一次读取一行,然后将该行分成数据的每一位? If so how would I do that? 如果是这样,我该怎么做?

Here is what I have so far in my main file: 到目前为止,这是我的主文件中的内容:

#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;
}

I've changed my code to implement the stringstream on the line string, but when I debug step by step my compiler crashes after executing this line: 我已经更改了代码以在行字符串上实现stringstream,但是当我逐步调试时,执行此行后编译器崩溃:

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

The error reads "Unhandled exception at 0x0F592208 (msvcp110d.dll) in safejogger.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC." 该错误显示为“ safejogger.exe中0x0F592208(msvcp110d.dll)处未处理的异常:0xC0000005:访问冲突写入位置0xCCCCCCCC。

Yes it is possible. 对的,这是可能的。 One way is to use a stringstream: http://www.cplusplus.com/reference/sstream/stringstream/ 一种方法是使用字符串流: http ://www.cplusplus.com/reference/sstream/stringstream/

Example: 例:

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++:如何从.txt文件的一行中读取每行中变量数量未知的行? - C++: How do I read in a line from a .txt file with an unknown number of variables on every other line? 如何从输入读取 CSV 行并将每个值初始化为不同类型的变量? (C++) - How do I read a CSV line from input and initialize each value to variables of different types? (C++) 如何从文本文件中读取数据并将其分配给变量? (C++) - How to read data from text file and assign it to variables? (C++) 如何从文件中读取复杂数据并输入到 C++ 中的数组 - How to read in complex data from file and input in to array in C++ 如何在C或C ++中从命令行读取多行输入? - How to read a multiple line input from command line in c or C++? 在C ++函数中,如何读取在main()中打开的输入文件? - In a C++ function how do I read from an input file that was opened in main()? 如何从标准输入的 K 行输入和每行 N 个元素从 C++ 文件中读取输入 - How to read input from a C++ file with K lines of input and N elements on each line from stdin 如何在同一行C ++上输入多个变量 - How can I input multiple variables on the same line C++ 从文本文件中逐行读取数据,在C ++中由多个分隔符分隔 - Read in data from text file line by line, separated by multiple delimiters in C++ 在C ++中从输入文件读取另一行 - Read another line from an input file in c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM