C++的I/O流
C++的I/O流是带有缓冲的,使用 get()/getline()等函数,了解缓冲对输入的影响
调用文件流类中的get()函数读文件内容,文件位置指示器会向文件尾部移动一个单位,get函数读的是一个字符,移动的单位是一个字节
win10系统,因为访问C盘需要管理员权限,使用ostream 对象无法在该盘创建文件。
C++17 标准 filesystem类库
filesystem,提供了path类,可以直接处理与文件路径相关的操作,也可以获取磁盘的相关信息
namespace fs = std::filesystem; fs::path p{ "CheckPath.cpp" };
namespace fs = std::filesystem; fs::path p1("d:\\cpp\\hi.txt"); // 字符串中的反斜杠要被转义 fs::path p2("d:/cpp/hi.txt"); // Windows也支持正斜杠 fs::path p3(R"(d:\cpp\hi.txt)");// 使用原始字符串字面量
Absolute Path (platform dependent) (绝对路径):
An absolute path contains a file name with its complete path and drive letter.(包含完整的路径和驱动器符号)
Relative Path (相对路径)
(1) Contains NO drive letter or leading "/" (不包含驱动器及开头的/符号)
(2) The file stores in the path Relative to "Current Path" (文件存在相对于“当前路径”的位置)
二进制输入输出
用write()和read()这两个函数,这两个函数接收的第一个参数是 char*类型的指针
将数组、整型变量等等与char*类型指针转换, reinterpret_cast<char*>(&x)
随机文件访问
文件读写
1 #include <iostream> 2 #include <fstream> 3 #include <string> 4 5 int main() 6 { 7 //1.向文件写数据 ofstream 8 std::ofstream out("d:\\test.txt"); 9 std::string name; 10 int i = 10; 11 //写数据 12 while(i--){ 13 std::cin>>name; //键盘输入字符串 14 out << name <<"\n"; //写入文件 15 } 16 //关闭文件 17 out.close(); 18 19 //2.从文件读数据 ifstream 20 std::ifstream in("d:\\test.txt"); 21 22 /*std::cin>>i; 23 if(i<1 || i>10){ 24 std::cout<<"error"<<std::endl; 25 exit(0); 26 } 27 //读数据 28 while (i--) { 29 in>>name; //读文件到字符串 30 std::cout<<name<<" "; //字符串输出到屏幕 31 } */ 32 33 while (in.eof() == false) { 34 std::cout << static_cast<char>(in.get()); 35 } 36 37 //关闭文件 38 in.close(); 39 40 return 0; 41 }
writing on a text file
1 // writing on a text file 2 #include <fstream> 3 using namespace std; 4 5 int main () { 6 ofstream out("out.txt"); //创建一个out.txt的文件 7 if (out.is_open()) 8 { 9 out << "This is a line.\n"; //写入文件 10 out << "This is another line.\n"; 11 out.close(); //关闭文件 12 } 13 return 0; 14 }
文件读写对象
#include <iostream> #include <fstream> class ObjectTest{ private: char str[20]; float f; int i; public: void set(){ std::cin >> str >> f >> i; } void display(){ std::cout << "string:" << str << ", float:" << f << ", int:" << i << std::endl; } }; int main() { ObjectTest ot; //对象ot //1.向文件写数据 ofstream std::ofstream out("d:\\test.dat"); //写数据 ot.set(); //设置对象的成员变量的值 out.write((char*)&ot, sizeof(ot));//按对象写入文件 //关闭文件 out.close(); //2.从文件读数据 ifstream std::ifstream in("d:\\test.dat"); //读数据 in.read((char*)&ot, sizeof(ot)); //读文件 ot.display(); //打印对象 //关闭文件 in.close(); return 0; }
文件拷贝
#include <iostream> #include <fstream> using namespace std; int main() { //打开 ifstream inf ("A.txt"); ofstream outf("B.txt"); //拷贝A->B char buf[128]; while(!inf.eof()){ // eof函数返回值,到文件尾为真 inf.getline(buf,128); outf << buf; } //关闭 inf.close(); outf.close(); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人