#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
int main()
{
void readtest();//声明
readtest();//调用
return 0;
}
void readtest()
{
void split_cpp(const string& str,vector<string>& vector_name,const string& segmentation_condition);//声明
string file,linetext;
vector<string> split_line;
file="../test.txt";//读取的文件路径已经文件名,其中"../"代表的是当前项目下的目录(相对路径)
ifstream readin(file);
if(!readin.is_open())//只读,如果打开文件失败.
{
cout<<"can't open the file!"<<endl;
}
while(getline(readin,linetext))//while循环,getline读行,每行存入linetext.
{
split_cpp(linetext,split_line," ");//调用函数,分割条件为空格
cout<<split_line.at(0)<<" "<<split_line.at(1)<<" "<<split_line.at(2)<<" "<<split_line.at(3)
<<" "<<split_line.at(4)<<endl;//输出split_line.
}
}
void split_cpp(const string& str,vector<string>& vector_name,const string& segmentation_condition)
//第一个参数为输入参数,读进来还没有操作的.第二个参数是分割完成后存入的数组名,第三个参数为分割条件
{
string ::size_type lastpos=str.find_first_not_of(segmentation_condition,0);//从0开始,第一个不是空格的位置
string ::size_type pos=str.find_first_of(segmentation_condition,lastpos);//从上一个不是空格的位置开始,第一个空格的位置
while (string::npos!=pos||string::npos!=lastpos)
{
vector_name.push_back(str.substr(lastpos,pos - lastpos));//取子串从lastpos位置开始,长度为pos-lastpos
lastpos=str.find_first_not_of(segmentation_condition,pos);//同上
pos=str.find_first_of(segmentation_condition,lastpos);//同上
}
}
使用非qt的c++实现,注意包含头文件以及使用命名空间.
另外,读取文件的方式也为c++和前文中的qt不同.
(另文中split_cpp函数是我在网上看到的,非原创,出处已忘记.)