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;
}


相关文章
|
8月前
|
C++ Windows
.NET Framework安装不成功,下载`NET Framework 3.5`文件,Microsoft Visual C++
.NET Framework常见问题及解决方案汇总,涵盖缺失组件、安装失败、错误代码等,提供多种修复方法,包括全能王DLL修复工具、微软官方运行库及命令行安装等,适用于Windows系统,解决应用程序无法运行问题。
1188 3
|
存储 算法 安全
基于哈希表的文件共享平台 C++ 算法实现与分析
在数字化时代,文件共享平台不可或缺。本文探讨哈希表在文件共享中的应用,包括原理、优势及C++实现。哈希表通过键值对快速访问文件元数据(如文件名、大小、位置等),查找时间复杂度为O(1),显著提升查找速度和用户体验。代码示例展示了文件上传和搜索功能,实际应用中需解决哈希冲突、动态扩容和线程安全等问题,以优化性能。
|
存储 程序员 C++
深入解析C++中的函数指针与`typedef`的妙用
本文深入解析了C++中的函数指针及其与`typedef`的结合使用。通过图示和代码示例,详细介绍了函数指针的基本概念、声明和使用方法,并展示了如何利用`typedef`简化复杂的函数指针声明,提升代码的可读性和可维护性。
|
算法 C语言
C语言中的文件操作技巧,涵盖文件的打开与关闭、读取与写入、文件指针移动及注意事项
本文深入讲解了C语言中的文件操作技巧,涵盖文件的打开与关闭、读取与写入、文件指针移动及注意事项,通过实例演示了文件操作的基本流程,帮助读者掌握这一重要技能,提升程序开发能力。
869 3
|
Linux C++
Linux c/c++文件的基本操作
在Linux环境下使用C/C++进行文件的基本操作,包括文件的创建、写入、读取、关闭以及文件描述符的定位。
260 0
Linux c/c++文件的基本操作
|
Linux C++
Linux c/c++文件虚拟内存映射
这篇文章介绍了在Linux环境下,如何使用虚拟内存映射技术来提高文件读写的速度,并通过C/C++代码示例展示了文件映射的整个流程。
488 0
|
Linux C++
Linux c/c++文件移动
这篇文章介绍了在Linux环境下,使用C/C++语言通过命令方式和文件操作方式实现文件移动的方法。
395 0
|
Serverless 编译器 C语言
【C语言】指针篇- 深度解析Sizeof和Strlen:热门面试题探究(5/5)
【C语言】指针篇- 深度解析Sizeof和Strlen:热门面试题探究(5/5)
252 0
|
Linux API C++
超级好用的C++实用库之文件目录操作
超级好用的C++实用库之文件目录操作
283 0

推荐镜像

更多
  • DNS