C++文件操作解析及使用(读、写文件 使用文件指针)

简介: C++文件操作解析及使用(读、写文件 使用文件指针)

进行文件I/O操作的时候要使用<fstream>头文件包含了很多标准库


1.下面是测试代码


创建文件并往文件中写入内容


当文件夹中没有下面的文件时会创建,并且会覆盖原文件重新写入


一般是创建在编译器的那个 文件夹里

#include<iostream>
#include<strstream>
#include<fstream>
#include<iomanip>
using namespace std;
int main() {
  /*
  ofstream ofile;
  cout << "create file1" << endl;
  ofile.open("test.txt");
  if (!ofile.fail())
  {
    ofile << "name1" << " ";
    ofile << "sex1" << " ";
    ofile << "age1" << " ";
    ofile.close();
    cout << "create file2" << endl;
    ofile.open("test2.txt");
    if (!ofile.fail()) {
      ofile << "name2" << " ";
      ofile << "sex2" << "";
      ofile << "age2" << "";
      ofile.close();
    }


2:下面的测试代码用了ifstream和ofstream对象实现读写文件的功能


用户手动输入5次数据 程序把它写入文件中 然后再打开

#include<iostream>
#include<strstream>
#include<fstream>
#include<iomanip>
using namespace std;
int main(){
char buf[128];
  ofstream ofile("test.txt");
  for (int i = 0; i < 5; i++) {
    memset(buf, 0, 128);//输入五次数据 程序把这五次数据写入文件中
    cin >> buf;
    ofile << buf;
  }
  ofile.close();//关闭
  ifstream ifile("test.txt");//然后再用ifstream打开
  while (!ifile.eof()) {
    char ch;
    ifile.get(ch);
    if (!ifile.eof())
      cout << ch;
  }
  cout << endl;
  ifile.close();
}

3:对二进制文件的读写 要用 ios::binary模式 读取需要read方法 写则需要write方法


#include<iostream>
#include<strstream>
#include<fstream>
#include<iomanip>
using namespace std;
int main(){
char buf[50];
  fstream file;
  file.open("test.dat", ios::binary | ios::out);
  for (int i = 0; i < 2; i++) {
    memset(buf, 0, 50);
    cin >> buf;
    file.write(buf, 50);//二进制写要用 write方法
    file << endl;
  }
  file.close();
  file.open("test.dat", ios::binary | ios::in);
  while (!file.eof())
  {
    memset(buf, 0, 50);
    file.read(buf, 50);//二进制读取要read方法
    if (file.tellg() > 0)
      cout << buf;
  }
  cout << endl;
  file.close();
  return 0;
}

4:文件的追加


在写入文件时 你可以不一次性写入全部数据 而是一部分一部分写 即不会覆盖前面的内容


测试代码如下


#include<iostream>
#include<fstream>
using namespace  std;
int main() {
  /*
  ofstream ofile("test.txt", ios::app);
  if (!ofile.fail()) {
    cout << "start write" << endl;
    ofile << "mary  ";
    ofile << "girl  ";
    ofile << "24  ";
  }
  else
    cout << "打不开啊";
}

5:eof成员函数可以用来判断文件结尾


在指定位置读写文件  用到了文件指针 可以输出你要求的位置的信息


1666417945537.jpg


测试代码如下

#include<iostream>
#include<fstream>
using namespace  std;
int main() {
ifstream ifile;
  char cfileselect[20];
  cout << "输入文件名:";
  cin >> cfileselect;
  ifile.open(cfileselect);
  if (!ifile)
  {
    cout << cfileselect << "打不开" << endl;
    return 0;
  }
  ifile.seekg(0, ios::end);
  int maxpos = ifile.tellg();
  int pos;
  cout << "输入查找的位置:";
  cin >> pos;
  if (pos > maxpos) {
    cout << "is over file lenght" << endl;
  }
  else
  {
    char ch;
    ifile.seekg(pos);
    ifile.get(ch);
    cout << ch << endl;
  }
  ifile.close();
  return 1;
}


相关文章
|
16天前
|
XML JavaScript 前端开发
xml文件使用及解析
xml文件使用及解析
|
27天前
|
存储 C++ 容器
C++入门指南:string类文档详细解析(非常经典,建议收藏)
C++入门指南:string类文档详细解析(非常经典,建议收藏)
34 0
|
27天前
|
存储 C++
基于C++的简易文件压缩与解压缩工具设计与实现
基于C++的简易文件压缩与解压缩工具设计与实现
15 3
|
1天前
|
C++
【C++11(三)】智能指针详解--RAII思想&循环引用问题
【C++11(三)】智能指针详解--RAII思想&循环引用问题
|
1天前
|
存储 人工智能 C++
【重学C++】【指针】详解让人迷茫的指针数组和数组指针
【重学C++】【指针】详解让人迷茫的指针数组和数组指针
13 1
|
5天前
|
XML C# 数据格式
C# 解析XML文件
C# 解析XML文件
14 1
|
16天前
|
存储 C++ iOS开发
C++文件操作(文本文件的读写+二进制文件的读写)
C++文件操作(文本文件的读写+二进制文件的读写)
|
16天前
|
存储 C++
C++指针
C++指针
|
21天前
|
C++
C++ While 和 For 循环:流程控制全解析
本文介绍了C++中的`switch`语句和循环结构。`switch`语句根据表达式的值执行匹配的代码块,可以使用`break`终止执行并跳出`switch`。`default`关键字用于处理没有匹配`case`的情况。接着,文章讲述了三种类型的循环:`while`循环在条件满足时执行代码,`do/while`至少执行一次代码再检查条件,`for`循环适用于已知循环次数的情况。`for`循环包含初始化、条件和递增三个部分。此外,还提到了嵌套循环和C++11引入的`foreach`循环,用于遍历数组元素。最后,鼓励读者关注微信公众号`Let us Coding`获取更多内容。
21 0
|
22天前
|
C++ Python
【C++/Python】C++调用python文件
【C++/Python】C++调用python文件

推荐镜像

更多