電商專業(yè)學(xué)習(xí)嵌入式軟件開發(fā)第六十三天

  • C++第九天

今天是C++的最后一天了,下周講數(shù)據(jù)庫,講完數(shù)據(jù)庫再講完qt,我們的課程就完成了,數(shù)據(jù)庫和qt是我比較感興趣的,畢竟沒有代碼那么枯燥,但是因為沒有打好基礎(chǔ),決定下周再到新班重新學(xué)一遍C基礎(chǔ),最起碼要能夠自己寫出來一個項目。今天主要講文件操作,然后就開始講項目,老師帶著我們迅速過了一個項目,然后又留了一個項目讓我們完成,老師說這個和第一個項目類似,所以兩天時間夠了,我想大部分同學(xué)的時間肯定夠了,而我就要到下個班重修了。

由iostream生成fstream,istream生成ifstream,ostream生成ofstream

#include <iostream>
#include <fstream>
using namespace std;

int main(void)
{
    ofstream ofile;
    //默認以寫的方式打開文件,若文件不存在,則自動創(chuàng)建文件
    ofile.open("test.data");
    if (ofile.is_open())
    {
        cout << "open ok\n";
        ofile << "Hello World\n" << 88888 << ' ' << 3.14567 << endl;
    }
    else
    {
        cout << "open failed\n";
    }

    return 0;
}
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <string.h>
using namespace std;

class Student
{
public:
    Student(){}
    Student(const char *name, float score=0)
    {
        strcpy(m_caName, name);
        m_fScore = score;
    }
    const char *getName()
    {
        return m_caName;
    }
    float getScore()
    {
        return m_fScore;
    }
    friend ofstream &operator<< (ofstream &file, const Student &stu);
friend ifstream &operator>> (ifstream &file, Student &stu);
private:
    char m_caName[32];
    float m_fScore;
};

ofstream &operator<< (ofstream &file, const Student &stu)
{
    file << stu.m_caName << ' ' << stu.m_fScore << endl;    
    return file;
}
ifstream &operator>> (ifstream &file, Student &stu)
{
    file >> stu.m_caName >> stu.m_fScore;   
    return file;
}
int main(void)
{
    ofstream ofile;
    ofile.open("stu.info");
//  ofstream ofile("stu.info");//ofstream自帶open函數(shù),相當(dāng)于上面兩行
    if (ofile.is_open())
    {
        cout << "open ok\n";
        Student stu("張三三", 89);
        ofile << stu << endl;
    }
    else
    {
        cout << "open failed\n";
    }

    return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <string.h>
#include <fstream>
using namespace std;
class Student
{
public:
    Student(){}
    Student(const char *name, float score=0)
    {
        strcpy(m_caName, name);
        m_fScore = score;
    }
    const char *getName()
    {
        return m_caName;
    }
    float getScore()
    {
        return m_fScore;
    }
    friend ofstream &operator<< (ofstream &file, const Student &stu);
friend ifstream &operator>> (ifstream &file, Student &stu);
private:
    char m_caName[32];
    float m_fScore;
};

ofstream &operator<< (ofstream &file, const Student &stu)
{
    file << stu.m_caName << ' ' << stu.m_fScore << endl;    
    return file;
}
ifstream &operator>> (ifstream &file, Student &stu)
{
    file >> stu.m_caName >> stu.m_fScore;   
    return file;
}
int main(void)
{
    ofstream ofile;
    ofile.open("stu.info");//以二進制形式讀寫文件
    if (ofile.is_open())
    {
        cout << "open ok\n";
        Student stu("張三", 99);
        ofile.write((char *)&stu, sizeof(stu));
        ofile.seekg(0, );//寫文件
        Student stu2;
        ofile.read((char*)&stu2, sizeof(stu2));
        cout << stu2.getName << ' ' << stu2.getScore()<< endl;
    }
    else
    {
        cout << "open failed\n";
    }

    return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <string.h>
using namespace std;

class Student
{
public:
    Student(){}
    Student(const char *name,float score = 0)
    {
        strcpy(m_caName,name);
        m_fScore = score;
    }
    const char *getName()
    {
        return m_caName;    
    }
    float getScore()
    {
        return m_fScore;
    }
    friend ofstream &operator << (ofstream &file,const Student &stu);
    friend ifstream &operator >> (ifstream &file,Student &stu);
private:
    char m_caName[32];
    float m_fScore;
};
ofstream &operator << (ofstream &file,const Student &stu)
{
    file << stu.m_caName << ' ' << stu.m_fScore << endl;
    return file;
}
ifstream &operator >> (ifstream &file,Student &stu)
{
    file >> stu.m_caName >> stu.m_fScore;
    return file;
}
int main(int argc,char *argv[])
{
    ofstream ofile;
    ofile.open("stu.info");//以二進制形式讀寫文件
    if(ofile.is_open())
    {
        cout << "open ok\n";
        Student stu("zhangsan",99);
        ofile.write((char *)&stu, sizeof(stu));
    
//      ofile.seekp();//寫文件

        ofile.close();
        ifstream ifile("stu.info");
        Student stu2;

//      ifile.seekp();//讀文件

        ifile.read((char *)&stu2,sizeof(stu2));
        ifile.close();
        cout << stu2.getName() << ' ' << stu2.getScore() << endl;
    }
    else
    {
        cout << "open failed\n";
    }
    return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <string.h>
using namespace std;

class Student
{
public:
    Student(){}
    Student(const char *name,float score = 0)
    {
        strcpy(m_caName,name);
        m_fScore = score;
    }
    const char *getName()
    {
        return m_caName;    
    }
    float getScore()
    {
        return m_fScore;
    }
    friend ofstream &operator << (ofstream &file,const Student &stu);
    friend ifstream &operator >> (ifstream &file,Student &stu);
private:
    char m_caName[32];
    float m_fScore;
};
ofstream &operator << (ofstream &file,const Student &stu)
{
    file << stu.m_caName << ' ' << stu.m_fScore << endl;
    return file;
}
ifstream &operator >> (ifstream &file,Student &stu)
{
    file >> stu.m_caName >> stu.m_fScore;
    return file;
}
int main(int argc,char *argv[])
{
    fstream file;
//  file.open("stu.info");//默認以讀寫方式打開文件會保留文件內(nèi)容
    file.open("stu.info",ios_base::in|ios_base::out|ios_base::trunc);//以讀\寫\截斷的方式打開文件會清空文件里面內(nèi)容
    if(file.is_open())
    {
        Student s1("lisi",78);
        file.write((char *)&s1,sizeof(s1));//向文件寫數(shù)據(jù)
        cout << file.tellp() << endl;//tellp文件開始位置

        Student s2;
        file.seekg(0,ios_base::beg);//讀數(shù)據(jù)
        file.read((char *)&s2,sizeof(s2));  
        cout << s2.getName() << ' ' << s2.getScore() << endl;
        file.close();
    
        cout << "open ok\n";
    }
    return 0;
}
#include <iostream>
#include <fstream>
using namespace std;

int main(void)
{
    ifstream ifile;
    //若文件不存在則不會創(chuàng)建文件
    ifile.open("newdata.info");
    if (ifile.is_open())
    {
        string str;
        int data;
        int data2;
        float data3;
        ifile >> str >> data >> data2 >> data3;
        cout << str << ' ' << data << ' ' << data2 << ' ' << data3 << endl;
        cout << "open ok\n";
    }
    else
    {
        cout << "open failed\n";
    }

    return 0;
}
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <string.h>
using namespace std;

class Student
{
public:
    Student(){}
    Student(const char *name, float score=0)
    {
        strcpy(m_caName, name);
        m_fScore = score;
    }
    const char *getName()
    {
        return m_caName;
    }
    float getScore()
    {
        return m_fScore;
    }
    friend ofstream &operator<< (ofstream &file, const Student &stu);
friend ifstream &operator>> (ifstream &file, Student &stu);
private:
    char m_caName[32];
    float m_fScore;
};

ofstream &operator<< (ofstream &file, const Student &stu)
{
    file << stu.m_caName << ' ' << stu.m_fScore << endl;    
    return file;
}
ifstream &operator>> (ifstream &file, Student &stu)
{
    file >> stu.m_caName >> stu.m_fScore;   
    return file;
}
int main(void)
{
    ifstream ifile;
    ifile.open("stu.info");
//      ifstream ifile("stu.info");//ifstream自帶open函數(shù),相當(dāng)于上面兩行
    if (ifile.is_open())
    {
        cout << "open ok\n";
        Student stu;
        ifile >> stu;
        cout << stu.getName() << ' ' << stu.getScore() << endl;
    }
    else
    {
        cout << "open failed\n";
    }

    return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <string.h>
#include <fstream>
using namespace std;

class Student
{
public:
    Student(){}
    Student(const char *name, float score=0)
    {
        strcpy(m_caName, name);
        m_fScore = score;
    }
    const char *getName()
    {
        return m_caName;
    }
    float getScore()
    {
        return m_fScore;
    }
    friend ofstream &operator<< (ofstream &file, const Student &stu);
friend ifstream &operator>> (ifstream &file, Student &stu);
private:
    char m_caName[32];
    float m_fScore;
};

ofstream &operator<< (ofstream &file, const Student &stu)
{
    file << stu.m_caName << ' ' << stu.m_fScore << endl;    
    return file;
}
ifstream &operator>> (ifstream &file, Student &stu)
{
    file >> stu.m_caName >> stu.m_fScore;   
    return file;
}
int main(void)
{
    fstream file;
//  file.open("stu.info");//默認以讀寫方式打開文件會保留文件內(nèi)容
    file.open("stu.info", ios_base::in|ios_base::out|ios_base::trunc);//以讀\寫\截斷的方式打開文件會清空文件里面內(nèi)容
    if (file.is_open())
    {
        Student *pstu = NULL;
        for (int i=0; i< 10; i++)
        {
            pstu = new Student("zhangsan", 60+i);
            file.write((char *)pstu,sizeof(Student));
        }
        Student tmp;
        file.seekg(0, ios_base::beg);
//        while (cin >> data)
        while (file.read((char*)&tmp,sizeof(Student)))
        {
            cout << tmp.getScore() << endl;
        }
//        while (!file.eof())
//        {
//            file.read((char*)&tmp,sizeof(Student));
//            if(!file.eof())
//            cout << tmp.getScore() << endl;
//        }
        file.close();
        cout << "open ok\n";
    }

    return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <string.h>
using namespace std;

class Student
{
public:
    Student(){}
    Student(const char *name, float score=0)
    {
        strcpy(m_caName, name);
        m_fScore = score;
    }
    const char *getName()
    {
        return m_caName;
    }
    float getScore()
    {
        return m_fScore;
    }
    friend ofstream &operator<< (ofstream &file, const Student &stu);
friend ifstream &operator>> (ifstream &file, Student &stu);
private:
    char m_caName[32];
    float m_fScore;
};

ofstream &operator<< (ofstream &file, const Student &stu)
{
    file << stu.m_caName << ' ' << stu.m_fScore << endl;    
    return file;
}
ifstream &operator>> (ifstream &file, Student &stu)
{
    file >> stu.m_caName >> stu.m_fScore;   
    return file;
}
int main(void)
{
    fstream file;
    file.open("stu.info", ios_base::in|ios_base::out|ios_base::trunc);
    if (file.is_open())
    {
        Student s1("zhangsan", 78);
        file.write((char *)&s1, sizeof(s1));
        cout << file.tellp() << endl;

        Student s2;
        file.seekg(0, ios_base::beg);
        file.read((char *)&s2, sizeof(s2));
        cout << s2.getName()<< ' ' << s2.getScore()<< endl;
        file.close();
        cout << "open ok\n";
    }
    return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <string.h>
using namespace std;

class Student
{
public:
    Student(){}
    Student(const char *name, float score=0)
    {
        strcpy(m_caName, name);
        m_fScore = score;
    }
    const char *getName()
    {
        return m_caName;
    }
    float getScore()
    {
        return m_fScore;
    }
    friend ofstream &operator<< (ofstream &file, const Student &stu);
friend ifstream &operator>> (ifstream &file, Student &stu);
private:
    char m_caName[32];
    float m_fScore;
};
ofstream &operator<< (ofstream &file, const Student &stu)
{
    file << stu.m_caName << ' ' << stu.m_fScore << endl;    
    return file;
}
ifstream &operator>> (ifstream &file, Student &stu)
{
    file >> stu.m_caName >> stu.m_fScore;   
    return file;
}
int main(void)
{
    ofstream ofile;
    ofile.open("stu.info");
    if (ofile.is_open())
    {
        cout << "open ok\n";
        Student stu("張三", 99);
        ofile.write((char *)&stu, sizeof(stu));
//        ofile.seekp();

        ofile.close();
        ifstream ifile("stu.info");
        Student stu2;
//        ifile.seekg();
        ifile.read((char*)&stu2, sizeof(stu2));
        ifile.close();
        cout << stu2.getName()<< ' ' << stu2.getScore()<< endl;
    }
    else
    {
        cout << "open failed\n";
    }

    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容