a_10_9.cpp

来自「C++应用教程原码,里面包含该书中有十三章内容的代码,详细具体」· C++ 代码 · 共 36 行

CPP
36
字号
#include "stdafx.h"
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

string readFile()
{ ifstream file("lesson.txt");
  ostringstream buffer;
  char ch;
   if(!file)
    { cerr << "文件打开失败"<<endl;
      exit(-1);
    }
  while (file.get(ch))
	  buffer.put(ch);
  return buffer.str();
}

string compound()
{ ostringstream buffer;
  int i = 1024;
  double j = 3.1415926;
  char *k = "go to swim";
  double *p = &j;
  buffer << i << " " << j << " " << k << " "
	     << "变量j的地址是"<< p;
  return buffer.str();
}

void main()
{  cout << "读取文件lesson的内容:"<< endl << readFile()<<endl<<endl;
   cout << "复合各种类型的数据:"<< endl << compound()<<endl;
   cin.get(); //等待结束,以便调测程序,可以删除
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?