首页 > 代码库 > 第3章 C++ I/O流技术

第3章 C++ I/O流技术

 

 

 

 

/*  第3章 C++ I/O流技术   3.1 I/O流类   3.2 标准输入输出   3.3 文件输入输出   3.4 流的格式控制   3.5 本章小结*///  第3章 C++ I/O流技术//   3.1 I/O流类 ------------------------------------------------------------------------------------------//   3.2 标准输入输出 ------------------------------------------------------------------------------------------// 35#include <iostream>int main(void){  int i, j;  std::cout << "请输入两个整数:\n";  std::cin >> i;  std::cin >> j;  std::cout << "两整数之和等于:\n";  std::cout << i + j;  return 0;}// 35-36#include <iostream>using namespace std;int main(void){  int i, j;  cout << "请输入两个整数" << endl;  cin >> i >> j;  cout << "两整数之和等于" << endl;  cout << i + j << endl;  return 0;}//   3.3 文件输入输出 ------------------------------------------------------------------------------------------// 37 ,如无文件,会自动创建#include <fstream>#include <iostream>using namespace std;int main(void){  ofstream wFileStream;  wFileStream.open("x:\\水果.txt", ios::out | ios::app);  if(!wFileStream)  {    cout << "打开文件失败" << endl;    return 1;  }  wFileStream << "apple" <<   << "苹果" << endl;  wFileStream << "banana" <<   << "香蕉" << endl;  wFileStream << "grape" <<   << "葡萄" << endl;  wFileStream.close();  return 0;}// 36-37,如无文件,会显示打开文件失败。#include <fstream>#include <iostream>using namespace std;int main(void){  ifstream rFileStream;  rFileStream.open("d:\\水果.txt", ios::in);  if(!rFileStream)  {    cout << "打开文件失败" << endl;    return 1;  }  //读文件  char eName[10], cName[10];  rFileStream >> eName >> cName;  cout << eName <<   << cName << endl;  rFileStream >> eName >> cName;  cout << eName <<   << cName << endl;  rFileStream >> eName >> cName;  cout << eName <<   << cName << endl;  rFileStream.close();  return 0;}// 38#include <fstream>#include <iostream>using namespace std;int main(void){  ifstream rFileStream;  rFileStream.open("d:\\水果.txt", ios::in | ios::binary);  if(!rFileStream)  {    cout << "打开文件失败" << endl;    return 1;  }  char c;  while(!rFileStream.eof())  {    rFileStream.get(c);    cout << c;  }  rFileStream.close();  return 0;}// 39#include <fstream>#include <iostream>using namespace std;struct Student{  char name[8];  int year;};int main(void){  Student st;  strcpy(st.name, "宋公明");  st.year = 18;  //  ofstream wFileStream;  wFileStream.open("d:\\student.txt", ios::out | ios::binary);  if(!wFileStream)  {    cout << "打开文件失败" << endl;    return 1;  }  wFileStream.write(reinterpret_cast < char * > (&st), sizeof(Student));  wFileStream.close();  //  ifstream rFileStream;  rFileStream.open("d:\\student.txt", ios::in | ios::binary);  rFileStream.read(reinterpret_cast < char * > (&st), sizeof(Student));  cout << st.name << "  " << st.year << endl;  rFileStream.close();  return 0;}// 39-40#include <fstream>#include <iostream>using namespace std;int main(void){  ifstream rFileStream;  rFileStream.open("d:\\水果.txt", ios::in);  if(!rFileStream)  {    cout << "打开文件失败" << endl;    return 1;  }  char str[30];  rFileStream.getline(str, 30);  cout << str;  rFileStream.close();  return 0;}// 40#include <fstream>#include <iostream>using namespace std;int main(void){  fstream rwFileStream;  rwFileStream.open("d:\\digit.txt", ios::in | ios::out | ios::binary | ios    ::trunc);  if(!rwFileStream)  {    cout << "打开文件失败" << endl;    return 1;  }  for(char i = 0; i < 26; i++)    rwFileStream << char(a + i);  //写入26个字母  //  rwFileStream.seekg(3, ios::beg); //定位到第3个字母处(从0索引)  char c;  rwFileStream.get(c);  cout << c << endl; //打印出字符d  rwFileStream.close();  return 0;}//   3.4 流的格式控制 ------------------------------------------------------------------------------------------// 42#include <iostream>using namespace std;int main(void){  cout.setf(ios::scientific | ios::uppercase);  cout << 2006.5 << endl;  //  cout.setf(ios::floatfield, ios::fixed | ios::showpos);  cout << 2006.5 << endl;  //  cout.setf(ios::showpos);  cout << 2006.5 << endl;  return 0;}//43#include <iostream>using namespace std;int main(void){  cout.width(20);  cout << "hello" << endl; //右对齐输出hello,占20个字符  //  cout.precision(4);  cout << 2006.526 << endl; //输出2007  //  cout.precision(5);  cout << 2006.526 << endl; //输出2006.5  //  cout.precision(7); // 设置有效位数  cout << 2006.526 << endl; //输出2006.526  //  cout.width(20);  cout.fill(*);  cout << "baby" << endl; //输出**************baby  return 0;}// 44-45#include <iostream>#include <iomanip>using namespace std;int main(void){  cout << setw(20) << "hello" << endl;  cout << setprecision(4) << 2006.526 << endl;  cout << setw(20) << setfill(*) << "baby" << endl;  cout << scientific << 300.89 << endl;  cout << oct << 9 << endl;  cout << dec << showpos << 172 << endl;  cout << noshowpos << 172 << endl;  cout << boolalpha << true << endl;  return 0;}// my test 。主要是为了测试浮点数的输出格式#include <iostream>//#include <iomanip>using namespace std;int main(void){  double d(3.141592653589);  cout << d << endl;  // 默认精度是六位有效数字  cout.precision(20); // 设置有效位数20位  cout << d << endl;  cout.precision(3);  // 保留三位小数  cout << fixed << d << endl; // fixed,浮点数以小数形式输出。有四舍五入功能    return 0;}//   3.5 本章小结 ------------------------------------------------------------------------------------------

 

 

 

 

 

 

 

TOP

 

第3章 C++ I/O流技术