c++ string处理比c 高端大气上档次
http://www.cnblogs.com/JCSU/articles/1190685.html
https://blog.csdn.net/kingstar158/article/details/6859379
ifstream 读文件到 string
1
ifstream fin("data.txt");
string s;
while( fin >> s ) //将fin作为源。输出给s //逐词读入字符串
{
cout << "Read from file: " << s << endl;
}
2 char str[100];
while( fin.getline(str,100) ) //和读文件一样,逐行读
{
cout << "Read from file: " << str << endl;
}
string s;
while( getline(fin,s) )
{
cout << "Read from file: " << s << endl;
}
============================================
1.0 准备环境1.0.1 upgrade gcc to 4.8https://teddysun.com/432.htmlhttp://www.cnblogs.com/highway-9/p/5628852.html1.0.2 c++11 build:g++ -std=gnu++11 -g isstringstream.cpp
1.1 数字转成string,强制转化 (c++ 有atoi,没有itoa)
stringstream ss;
double f=98.012345;
ss=f;
1.2 istringstream >> 替代find+sprintf
example string:57344 block groups
istringstream record(line); string name;
//这样可以从long string中直接提取第一个short string。连split都不用了. 必须是istringstream; line>> name is wrong; >>一步完成find+sprintf
record >> name;
1.3
1.3.1 stringstream and fstream (i is input ,o is output)
while (percent <=98){ sleep (10);
//每次loop。将重新装载最新的file(format_disk.log is 不停增长的);不用c的fopen+while read line by line std::ifstream ifs(file.c_str(), std::fstream::in);
....
while (length)
{
//直接到最后,index是负数,从后向前查找; 和c一样
ifs.seekg (index, ifs.end);
//string convert to double
current=atof(va1[0].c_str());
#include <sstream>
#include <string>
#include <vector>
#include <iostream>
#include <ostream>
using namespace std;
struct PersonInfo {
string name;
vector<string> phones;
};
//it is small database
main()
{
//line is line string, word is short string, it is word; both are string
string line, word;
vector<PersonInfo> people;
ostream &os=std::cout;
cout <<"input name , phones"<<endl;
while(getline(cin,line)) {
if (line=="EOF")
break;
PersonInfo info;
istringstream record(line);
//read info.name ; it means read a string
record >> info.name;
cout <<"info.name="<<info.name<<endl;
//loop read string
while (record >>word)
{
//one person can have more than 1 phone
info.phones.push_back(word);
}
//people is vector; phones is also vector; for many people , many phones; add vector element
people.push_back(info);
}
//iterator vector ; it is just for c11; it is the same as java syntax
for (const auto &entry :people) {
//compare to istringstream. it like snprintf; string output to ostringstream;
ostringstream formatted ,badNums;
for (const auto &nums : entry.phones)
formatted<<nums;
os <<entry.name<<":"<<formatted.str()<<endl;
}
}
}
stringstream ss;
//digit convert to string ;
ss <<percent; c没有itoa,不需要c根据ascii,0+48=‘0‘’ progress = ss.str();
}
1.4 ostream &os
https://segmentfault.com/q/1010000007297802/a-1020000007299015
1)输出的标准长相就是引用,引用和output要绑定
2)它是基类,不允许创建对象,只允许创建指针
3) init 使用它
std::ofstream os(filename.c_str());
ex1: 小数据库 (引自 c++ primer)
abc 15826612345
efg 15912345678 13312345678
常用打log方法:
//array don;t output like this . 1,3 method are right
1 char outbufnew[100];
ofstream outfile("f1.dat");
for (int i=0;i<len+headlen+1;i++)
outfile.put(outbufnew[i]);
outfile.close();
2 ofstream outfile2("f2.dat");
outfile2<<outbufnew<<endl;
outfile2.close();
3
FILE *fp = NULL;
char s[] = "f3.dat";
fp = fopen(s, "w+");
1.5 fstream
算法:从倒数第一行的倒数1个字母,while读到倒数第二行\n为止。然后getline向下一读一行.
用c实现了将光标向上移动.
#include <fstream>
#include <iostream>
#include <sstream>
#include <string.h>
#include <vector>
using namespace std;
main()
{
string file = "1.txt";
double current, total;
std::ifstream ifs(file.c_str(), std::fstream::in);
string demi1;
vector <string> va1;
if (!ifs)
cout <<"open file error"<<endl;
ifs.seekg(0, ifs.end);
int length = ifs.tellg();
std::string line;
//负数从后向前
int index = -2;
while(length)
{
char c;
ifs.seekg (index, ifs.end);
ifs.get(c);
cout <<"index="<<index<<"length="<<length<<"c="<<c<<endl;
if(c == '\n')
{
std::getline(ifs, line);
cout <<"line="<<line<<endl;
std::istringstream iss(line);
break;
}
length--;
index--;
}
}