c++ string format

来源:互联网 发布:ppt转换视频软件 编辑:程序博客网 时间:2024/06/05 09:19
利用stringstream类
stringstream ss;
ss<<"hhh"<<value<<"\t"<<"hh";

string str=ss.str();

code:

#include <iostream>#include <fstream>#include<string>#include<sstream>using namespace std;int main () {  ofstream ofile ("D:\\example.txt");  int value=123;  stringstream ss;  if (ofile.is_open())  {    /*ofile << "This is a line.\n";    ofile << "This is another line.\n";*/ ss<<"hhh"<<value<<"\t"<<"hh"; ofile<<ss.str();    ofile.close();  }  else cout << "Unable to open file";  string line;  ifstream ifile ("D:\\example.txt");  if (ifile.is_open())  {    while ( getline (ifile,line) )    {      cout << line << '\n';    }    ifile.close();  }  else cout << "Unable to open file";   return 0;}