• 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类型数据,用注意文件中的空格

  • 相关阅读:
    RSA
    antd 规则检查
    antd 使用总结问题
    react context prop-types
    【CSS/JS】如何实现单行/多行文本溢出的省略(...)
    react prop-types
    js 监听URL的hash变化
    Spark 读取Hadoop集群文件
    HIVE 常见函数
    Linux ANSI转 UTF8
  • 原文地址:https://www.cnblogs.com/lscheng/p/2220321.html
Copyright © 2020-2023  润新知