读取文本文件并将数据存储到多个数组 C++
Reading a text file and storing data into multiple arrays C++
我正在尝试读取一个数据库文件(作为 txt),我想在其中跳过空行并跳过文件中的 header 列行并将每条记录存储为数组。我想使用 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 值,但现在我想获得停止名称值,而且我对 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;
}
您可以将文件的整行读入一个字符串,然后使用 stringstream 一次一个地为您提供每个部分,直到并排除逗号。然后你可以填满你的数组。我假设您想要它自己的数组中的每一行并且您想要无限的数组。最好的方法是拥有一个数组数组。
std::string Line;
std::array<std::array<string>> Data;
while (std::getline(infile, Line))
{
std::stringstream ss;
ss << Line;
Data.push_back(std::vector<std::string>);
std::string Temp;
while (std::getline(ss, Temp, ','))
{
Data[Data.size() - 1].push_back(Temp);
}
}
通过这种方式,您将拥有一个充满向量的向量,每个向量都包含该行中所有数据的字符串。要将字符串作为数字访问,您可以使用 std::stoi(std::string)
将字符串转换为整数。
您可以使用string::find
3次来搜索第三次出现的逗号,并且您必须存储在line
中找到的最后2次出现的位置,然后将它们用作输入数据使用 string::substr
并获取搜索到的文本:
std::string line ("17,17,\"Jackson & Kolmar\",\"Jackson & Kolmar, Eastbound, Southeast Corner\",41.87685748,-87.73934698,0,,1");
std::size_t found=0, foundBack;
int i;
for(i=0;i<3 && found!=std::string::npos;i++){
foundBack = found;
found=line.find(",",found+1);
}
std::cout << line.substr(foundBack+1,found-foundBack-1) << std::endl;
我正在尝试读取一个数据库文件(作为 txt),我想在其中跳过空行并跳过文件中的 header 列行并将每条记录存储为数组。我想使用 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 值,但现在我想获得停止名称值,而且我对 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;
}
您可以将文件的整行读入一个字符串,然后使用 stringstream 一次一个地为您提供每个部分,直到并排除逗号。然后你可以填满你的数组。我假设您想要它自己的数组中的每一行并且您想要无限的数组。最好的方法是拥有一个数组数组。
std::string Line;
std::array<std::array<string>> Data;
while (std::getline(infile, Line))
{
std::stringstream ss;
ss << Line;
Data.push_back(std::vector<std::string>);
std::string Temp;
while (std::getline(ss, Temp, ','))
{
Data[Data.size() - 1].push_back(Temp);
}
}
通过这种方式,您将拥有一个充满向量的向量,每个向量都包含该行中所有数据的字符串。要将字符串作为数字访问,您可以使用 std::stoi(std::string)
将字符串转换为整数。
您可以使用string::find
3次来搜索第三次出现的逗号,并且您必须存储在line
中找到的最后2次出现的位置,然后将它们用作输入数据使用 string::substr
并获取搜索到的文本:
std::string line ("17,17,\"Jackson & Kolmar\",\"Jackson & Kolmar, Eastbound, Southeast Corner\",41.87685748,-87.73934698,0,,1");
std::size_t found=0, foundBack;
int i;
for(i=0;i<3 && found!=std::string::npos;i++){
foundBack = found;
found=line.find(",",found+1);
}
std::cout << line.substr(foundBack+1,found-foundBack-1) << std::endl;