zoukankan      html  css  js  c++  java
  • C++文件操作

    今天写了个C++的文件操作,这是最基本的,而这也足够我的应用了,这样可以讲视频处理中每一帧的处理时间写到文件中去。

     1 #include <iostream>
    2 #include <fstream>
    3
    4 using namespace std;
    5
    6 void main()
    7 {
    8 ofstream fileOutput("file.txt",ios::app);
    9 if(fileOutput.is_open())
    10 {
    11 fileOutput<<"This is the first file operation."<<endl;
    12 fileOutput<<"Second line."<<endl;
    13 fileOutput.close();
    14 }
    15
    16 cout<<"write finished."<<endl;
    17 system("pause");
    18
    19 ifstream fileInput("file.txt");
    20 char buffer[100];
    21 while(fileInput.getline(buffer, 100))
    22 {
    23 cout<<buffer<<endl;
    24 }
    25 }

    文件要包含fstream头文件,也是在std命名空间中开始编写程序。

    然后是文件的读入:首先要定义一个ofstream类型的对象,ofstream file(filename,打开方式),其中的filename可以加上文件的路径。

    用filename.is_open可以检查是否打开,用filename<<string<<可以在文件中读入数据。

    同样可以读出文件中的内容,定义ifstream类型的对象,用getline可以得到文件中的一行数据。

    最后需要注意的是,如果是double类型数据,用注意文件中的空格

  • 相关阅读:
    PAT 甲级 1132 Cut Integer (20 分)
    AcWing 7.混合背包问题
    AcWing 9. 分组背包问题
    AcWing 5. 多重背包问题 II
    AcWing 3. 完全背包问题
    AcWing 4. 多重背包问题
    AcWing 2. 01背包问题
    AcWing 875. 快速幂
    AcWing 874. 筛法求欧拉函数
    AcWing 873. 欧拉函数
  • 原文地址:https://www.cnblogs.com/lscheng/p/2220321.html
Copyright © 2011-2022 走看看