【发布时间】:2017-09-17 19:40:37
【问题描述】:
我正在尝试读取一个数据库文件(作为 txt),我想在其中跳过空行并跳过文件中的列标题行并将每条记录存储为一个数组。我想使用 stop_id 并适当地找到 stop_name。即
如果我说让我停止 17,程序将获得“Jackson & Kolmar”。
文件格式如下:
17,17,"Jackson & Kolmar","Jackson & Kolmar, Eastbound, Southeast Corner",41.87685748,-87.73934698,0,,1
18,18,"Jackson & Kilbourn","Jackson & Kilbourn, Eastbound, Southeast Corner",41.87688572,-87.73761421,0,,1
19,19,"Jackson & Kostner","Jackson & Kostner, Eastbound, Southeast Corner",41.87691497,-87.73515882,0,,1
到目前为止,我能够获取 stop_id 值,但现在我想获取 stop 名称值,并且对 c++ 字符串操作相当陌生
mycode.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string filename;
filename = "test.txt";
string data;
ifstream infile(filename.c_str());
while(!infile.eof())
{
getline(infile,line);
int comma = line.find(",");
data = line.substr(0,comma);
cout << "Line " << count << " "<< "is "<< data << endl;
count++;
}
infile.close();
string sent = "i,am,the,champion";
return 0;
}
【问题讨论】:
-
stackoverflow.com/a/5605159/8491726 你可以给 getline 定界符 - 所以它会读到逗号而不是整行