读取txt数据文件算法
1.istringstream
【C++】使用istringstream根据分隔符分割字符串 - Flix - 博客园
istream& getline ( istream &is , string &str , char delim );
第一个参数为 is 表示一个 istringstream,第二个参数表示我们要将字符串分割的结果通过 getline 逐个放入 str 中,第三个参数为分隔符,注意分隔符只能为字符型,默认的是空格分隔,如果是逗分割,即',',而不是字符串","。
C++ getline():从文件中读取一行字符串_睿科知识云的博客-CSDN博客_c++从文件读取一行
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
string s = "a,b,c,d";
istringstream iss(s);
string buffer;
while(getline(iss, buffer, ','))
//while(getline(iss, buffer, ' '))
{
cout<<buffer<<endl;
}
return 0;
}
C++中使用stringstream与getline处理一行被空格(逗号)隔开的数据_发如雪Jay的博客-CSDN博客_stringstream遇到空格
while (getline(fileStream, rowString, '\n'))//读取一行
{
std::istringstream is(rowString);
while (is >> colString)
{
//使用空格来划分句子
tmpV.push_back(std::stod(colString));
}
2.fscanf()
C语言从txt文本中读取多行用逗号分隔的数据_yanxiaopan的博客-CSDN博客_c语言读取逗号分隔的数字
#include <stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp = fopen("data.txt", "r");
if (fp == NULL)
{
printf("file open error\n");
return -1;
}
double xx[3];
double yy[3];
double zz[3];
for (int i = 0; i < 3; i++)
{
fscanf(fp, "%lf,%lf,%lf", &xx[i], &yy[i], &zz[i]);
printf("%lf,%lf,%lf\n", xx[i], yy[i], zz[i]);
}
fclose(fp);
system("pause");
return 0;
}
C语言格式说明符
C语言格式说明符_captain9911的博客-CSDN博客_c语言格式说明符
C语言读取文件(二)——fscanf 详谈_生信了(公众号同名)的博客-CSDN博客_c语言fscanf
1. fscanf 对空格的处理;
2. fscanf 对制表符的处理;
3. fscanf 对换行符的处理;
4. 当空格、制表符以及换行符混杂时fscanf的处理;
5. []符号在format str中的应用;
6. 出错的情况。
3.boost