C++读书笔记之 文件与流 文件读写操作 通过指针获取文件大小

在本例程中不仅概括了文件的基本读写操作,如何获取文件的大小,还牵扯到getline和cin.getline的区别,及string类字符串如何转换为c风格字符串的问题,这些问题将在下一篇博文中介绍。首先先看程序运行结果,运行结果如下:

/**********************************
程序运行结果如下:
Writing to the file
Enter your name: wangshihui
Enter your age: 22
Reading from the file
wangshihui
22
wangshihui

22

To recap, the three main objectives in the Mystery Method are:
            To attract a woman
            To establish comfort, trust, and connection
            To structure the opportunity to be seduced
A woman's number-one emotional priority is safety and security.

TheMisteryMethod.txt size is: 294 bytes.

Process returned 0 (0x0)   execution time : 13.100 s
Press any key to continue.

***********************************/



#include <fstream>
#include <iostream>
using namespace std;
void test_write_read_cin_getline()
{
    char data[100];
    // open a file in write mode.
    ofstream outfile;
    outfile.open("TheMisteryMethod.txt");
    cout << "Writing to the file" << endl;
    cout << "Enter your name: ";
    cin.getline(data, 100);
    // write inputted data into the file.
    outfile << data << endl;
    cout << "Enter your age: ";
    cin >> data;
    cin.ignore();
    // again write inputted data into the file.
    outfile << data << endl;
    // close the opened file.
    outfile.close();
    // open a file in read mode.
    ifstream infile;
    infile.open("TheMisteryMethod.txt");
    cout << "Reading from the file" << endl;
    infile >> data;
    // write the data at the screen.
    cout << data << endl;
    // again read the data from the file and display it.
    infile >> data;
    cout << data << endl;
    // close the opened file.
    infile.close();
}
void test_write()
{
  ofstream myfile;
  myfile.open ("TheMisteryMethod.txt",ios::app);
  if(myfile.is_open())
  {
     myfile <<
            "\nTo recap, the three main objectives in the Mystery Method are: \n\
            To attract a woman \n\
            To establish comfort, trust, and connection \n\
            To structure the opportunity to be seduced \n";
    myfile.close();
  }
  else
    cout<<"打开文件失败!\n";

}
void  test_write_read_getline()
{
  string str;

  //Creates an instance of ofstream, and opens TheMisteryMethod.txt
  ofstream a_file ( "TheMisteryMethod.txt",ios::app );//追加方式
  // Outputs to TheMisteryMethod.txt through a_file
  if(a_file.is_open())
  {
        a_file<<"A woman's number-one emotional priority is safety and security.";
        // Close the file stream explicitly
        a_file.close();
  }
   else
        cout << "Unable to open file\n";

  //Opens for reading the file  追加方式
  ifstream b_file ( "TheMisteryMethod.txt",ios::app );
  //Reads one string from the file
  b_file>> str; //只显示to 表示遇到空格停止接收字符
  cout<< str <<"\n";
  getline(b_file,str,'\0');//输出缓冲区剩余的字符
  cout<< str <<"\n";
  cin.get();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值