您的位置:首页 > 其它

myFile

2016-03-23 17:37 281 查看
#include <iostream>

#include <fstream> // c++ 文件IO头

#include <string>

#include<vector>

#include <string>

using namespace std;

#include "test.h"

int main(){

string strPath = "c:/Users/sjyt/Desktop/bpc";
string strTxtName = "/config.txt";
string str = strPath + strTxtName;
const char* path = str.c_str(); // 你要创建文件的路径
ofstream fout(path);
if (fout) { // 如果创建成功
fout << "写入内容\nhehehehehe" << endl; // 使用与cout同样的方式进行写入
fout.close();  // 执行完操作后关闭文件句柄
}

ifstream in(path);
string s;
while (getline(in, s))//着行读取数据并存于s中,直至数据全部读取
cout << s.c_str() << endl;

string str2 = ",1,2,3,,,";
test* tet = new test();
vector<string> vecS = tet->split(str2,",");

vecS.size();

delete tet;
return 0;

}

vector<string> test::split(const string& str, string sep )

{
vector<string> ret_;
if (str.empty())
{
return ret_;
}

string tmp;
string::size_type pos_begin = str.find_first_not_of(sep);
string::size_type comma_pos = 0;

while (pos_begin != string::npos)
{
comma_pos = str.find(sep, pos_begin);
if (comma_pos != string::npos)
{
tmp = str.substr(pos_begin, comma_pos - pos_begin);
pos_begin = comma_pos + sep.length();
}
else
{
tmp = str.substr(pos_begin);
pos_begin = comma_pos;
}

if (!tmp.empty())
{
ret_.push_back(tmp);
tmp.clear();
}
}

return ret_;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: