C++从文本文件中读取数据
我有以下数据:C++从文本文件中读取数据,c++,string,C++,String,我有以下数据: $GPVTG,,T,,M,0.00,N,0.0,K,A*13 我需要读取数据,但是逗号之间有空格,因此我不确定应该如何读取数据 另外,如何仅为一组数据选择GPVTG?例如: GPVTG,,T,,M GPGGA,184945.00 GPRMC,18494 GPVTG,,T,,M,0 GPGGA,184946.000,3409 我试过使用: /* read data line */ fgets(gpsString,100,gpsHandle); char type[10]
$GPVTG,,T,,M,0.00,N,0.0,K,A*13
我需要读取数据,但是逗号之间有空格,因此我不确定应该如何读取数据
另外,如何仅为一组数据选择GPVTG?例如:
GPVTG,,T,,M
GPGGA,184945.00
GPRMC,18494
GPVTG,,T,,M,0
GPGGA,184946.000,3409
我试过使用:
/* read data line */
fgets(gpsString,100,gpsHandle);
char type[10] = "GPVTG";
sscanf(gpsString," %GPVTG", &type);
if (strcmp(gpsString, "GPTVG") == 0){
printf("%s\n",gpsString);
}
如果您想使用基于FSRAND:
的C++风格代码 这个怎么样#include <istream>
#include <sstream>
class CSVInputStream {
public:
CSVInputStream(std::istream& ist) : input(ist) {}
std::istream& input;
};
CSVInputStream& operator>>(CSVInputStream& in, std::string& target) {
if (!in.input) return in;
std::getline(in.input, target , ',');
return in;
}
template <typename T>
CSVInputStream& operator>>(CSVInputStream& in, T& target) {
if (!in.input) return in;
std::string line;
std::getline(in.input, line , ',');
std::stringstream translator;
translator << line;
translator >> target;
return in;
}
//--------------------------------------------------------------------
// Usage follow, perhaps in another file
//--------------------------------------------------------------------
#include <fstream>
#include <iostream>
int main() {
std::ifstream file;
file.open("testcsv.csv");
CSVInputStream input(file);
std::string sentence_type;
double track_made_good;
char code;
double unused;
double speed_kph;
char speed_unit_kph;
double speed_kmh;
char speed_unit_kmh;
input >> sentence_type >> track_made_good >> code;
input >> unused >> unused;
input >> speed_kph >> speed_unit_kph;
input >> speed_kmh >> speed_unit_kmh;
std::cout << sentence_type << " - " << track_made_good << " - ";
std::cout << speed_kmh << " " << speed_unit_kmh << " - ";
std::cout << speed_kph << " " << speed_unit_kph << std::endl;;
}
这将逗号分隔与值的读取分开,并且可以在上重复使用
大多数其他逗号分隔的东西。这就是我要做的
#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
vector<string> &split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
int main()
{
ifstream ifs("file.txt");
string data_string;
while ( getline( ifs, data_string ) )
{
//i think you'd want to erase first $ charachter
if ( !data_string.empty() ) data_string.erase( data_string.begin() );
//now all data put into array:
vector<string> data_array = split ( data_string, ',' );
if ( data_array[0] == "GPVTG" )
{
//do whatever you want with that data entry
cout << data_string;
}
}
return 0;
}
你应该处理好你的任务。数组中的所有空元素都将是空字符串。问问你还需要什么
拆分功能的积分属于答案。请澄清。逗号之间的空格是什么?我在您提供的样本数据中没有看到任何空白。你是指CSV记录中的空字段吗?你已经标记了C++,但是张贴了C++,你想在哪个答案?@ JA103我认为他是指它们之间的,如果C类型的文件处理是必要的,你应该用C.来标记它,如果有可能使用流,你可以省略它。您应该研究如何使用std::string、std::getline和std::istringstream。StackOverflow中有许多使用它们的示例。
#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
vector<string> &split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
int main()
{
ifstream ifs("file.txt");
string data_string;
while ( getline( ifs, data_string ) )
{
//i think you'd want to erase first $ charachter
if ( !data_string.empty() ) data_string.erase( data_string.begin() );
//now all data put into array:
vector<string> data_array = split ( data_string, ',' );
if ( data_array[0] == "GPVTG" )
{
//do whatever you want with that data entry
cout << data_string;
}
}
return 0;
}