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

  • 相关阅读:
    链接和作用域2 C++快速入门43
    位运算符
    代码编辑器和代码浏览器
    关系运算符
    delphi教程 | 第一个程序
    代码编辑器和代码浏览器
    链接和作用域2 C++快速入门43
    delphi教程 | 第一个程序
    位运算符
    [原创 js] 点击即可修改内容函数
  • 原文地址:https://www.cnblogs.com/lscheng/p/2220321.html
Copyright © 2011-2022 走看看