C++ 语法文件

程序运行时产生的数据都属于临时数据,程序结束就会被释放。

通过文件可以可以将数据持久化

c++中对文件操作需要包含头文件fstream

文件的类型分为两种

1.文本文件 文件以文本的ASCII码形式存储在计算机中

2.二进制文件 稳重以文本的二进制形式存储在计算机中 用户一般不能直接读懂

操作文件的三大类

ofstream

ifstream

fstream

写文件

1.包含头文件

#include <fstream>

2.创建对象

ofstream ofs

3.打开文件

ofs.open("path","打开方式")

4.写数据

 ofs<<"写入数据"

5.关闭数据

ofs.close()

#include <iostream>
#include <fstream>
using namespace std;
void test65() {
    ofstream ofs;
    ofs.open("/Users/king/Documents/cpp/cpplearn/cpplearn/test.txt",ios::out);
    ofs << "12345677" << endl;
    ofs.close();
    
}
int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    test65();
    std::cout << "end!\n";
    return 0;
}

文件操作必须包含fstream

读文件可以利用ofstream  或者fstream类

打开文件时候需要指定操作文件的路径 以及打开方式

利用<<可与你向文件中写数据

操作完毕需要关闭文件

读文件

1.包含头文件

#include <fstream>

2.创建对象

ofstream ifs

3.打开文件

ifs.open("path","打开方式")

4.读数据

四种读取方式

5.关闭文件

ifs.close();

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void test65() {
    ofstream ofs;
    ofs.open("/Users/Documents/cpp/cpplearn/cpplearn/test.txt",ios::out);
    ofs << "12345677" << endl;
    ofs.close();
    
}
void test66() {
    ifstream ifs;
    ifs.open("/Users/cpp/cpplearn/cpplearn/test.txt",ios::in);
    if (!ifs.is_open()) {
        cout << "文件打开失败" << endl;
        return;
    }

    char buf[1024] = {0};
    // 方式1
//    while (ifs >> buf) {
//        cout << buf << endl;
//    }
    //2.
//    while (ifs.getline(buf, sizeof(buf))) {
//        cout << buf << endl;
//    }
    //3.
//    string buffer;
//    while (getline(ifs, buffer)) {
//        cout << buffer << endl;
//    }
    //4. 效率低下
    char c;
    while ( (c = ifs.get()) != EOF ) {
        cout << c << endl;
    }
    ifs.close();
    
}
int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    test65();
    test66();
    std::cout << "end!\n";
    return 0;
}

二进制读写

void test67() {
    ofstream ofs;
    ofs.open("/Users/xxx/Documents/cpp/cpplearn/cpplearn/test.txt",ios::out | ios::binary);
    Student1 s = {"王武",18};
    ofs.write((const char*)&s, sizeof(Student1));
    ofs.close();
}

void test69() {
    ifstream ifs;
    ifs.open("/Users/xxx/Documents/cpp/cpplearn/cpplearn/test.txt",ios::in | ios::binary);
    if (!ifs.is_open()) {
        std::cout << "is_open, failed!\n";
        return;
    }
    Student1 s;
    ifs.read((char *)&s, sizeof(Student1));
    cout << "name=" << s.m_name << "age = " << s.m_age << endl;
    ifs.close();
    
}

相关推荐

  1. C语言文件

    2024-02-06 23:40:02       11 阅读
  2. C语言——文件

    2024-02-06 23:40:02       7 阅读
  3. C语言——float.h文件

    2024-02-06 23:40:02       32 阅读
  4. C语言文件读写

    2024-02-06 23:40:02       23 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-06 23:40:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-06 23:40:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-06 23:40:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-06 23:40:02       18 阅读

热门阅读

  1. C语言——S/顺序表专题

    2024-02-06 23:40:02       33 阅读
  2. SpringMVC-请求

    2024-02-06 23:40:02       33 阅读
  3. Vue动态组件<component>传递变量

    2024-02-06 23:40:02       35 阅读
  4. api接口是什么意思,api接口该如何防护呢?

    2024-02-06 23:40:02       33 阅读
  5. 在Unix/Linux上使用通用二进制文件安装MySQL

    2024-02-06 23:40:02       30 阅读
  6. docker 的常用命令

    2024-02-06 23:40:02       36 阅读
  7. linux环境nginx安装及使用

    2024-02-06 23:40:02       31 阅读