美文网首页
c++ 读写文件

c++ 读写文件

作者: gaookey | 来源:发表于2020-08-26 19:04 被阅读0次
    image.png
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main() {
        
        //读
        //ifstream inFile;
        //inFile.open("/Users/mouos/Desktop/data.txt", ios::in)
        
        //ifstream inFile("/Users/mouos/Desktop/data.txt", ios::in);
        ifstream inFile("/Users/mouos/Desktop/data.txt");
        if (inFile) {
            cout<<"打开成功"<<endl;
            inFile.close();
        } else cout<<"打开失败"<<endl;
        
        //写
        //ofstream outFile;
        //outFile.open("/Users/mouos/Desktop/data.txt", ios::out | ios::binary);
        
        //ofstream outFile("/Users/mouos/Desktop/data.txt", ios::out | ios::binary);
        ofstream outFile("/Users/mouos/Desktop/data.txt", ios::out);
        if (outFile) {
            cout<<"打开成功"<<endl;
            inFile.close();
        } else cout<<"打开失败"<<endl;
        
        //读写
        fstream file("/Users/mouos/Desktop/data.txt", ios::out | ios::in);
        if (outFile) {
            cout<<"打开成功"<<endl;
            inFile.close();
        } else cout<<"打开失败"<<endl;
        
        return 0;
    } 
    

    写文件文本

    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main() {
        
        char id[11], name[21];
        int score;
        ofstream outFile;
        outFile.open("/Users/mouos/Desktop/data.txt");
        if (!outFile) {
            cout<<"创建文件失败"<<endl;
            return 0;
        }
        
        cout<<"输入:学号 姓名 成绩(ctrl+Z结束输入,mac下control+D结束输入)"<<endl;
        while (cin>>id>>name>>score) {
            outFile<<id<<" "<<name<<" "<<score<<endl;
        }
        outFile.close();
        
        return 0;
    } 
    

    读文件文本

    #include <iostream>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    int main() {
        
        char id[11], name[21];
        int score;
        ifstream inFile;
        inFile.open("/Users/mouos/Desktop/data.txt");
        if (!inFile) {
            cout<<"打开文件失败"<<endl;
            return 0;
        }
        cout<<"学号 姓名 成绩\n";
        while (inFile>>id>>name>>score) {
            cout<<left<<setw(10)<<id<<" "<<setw(10)<<name<<" "<<setw(10)<<right<<score<<endl;
        }
        inFile.close();
        
        return 0;
    } 
    

    读写二进制文件

    #include <iostream>
    #include <fstream>
    using namespace std;
    
    class Student {
      
    public:
        char id[11];
        char name[21];
        int score;
    };
    
    int main() {
        
        Student stu;
        ofstream outFile("/Users/mouos/Desktop/data.dat", ios::out | ios::binary);
        if (!outFile) {
            cout<<"创建文件失败"<<endl;
            return 0;
        }
        
        while (cin>>stu.id>>stu.name>>stu.score) {
            outFile.write((char*)&stu, sizeof(stu));
        }
        outFile.close();
        
        return 0;
    } 
    

    相关文章

      网友评论

          本文标题:c++ 读写文件

          本文链接:https://www.haomeiwen.com/subject/pymojktx.html