網頁

2016年12月2日 星期五

Ex10 struct與文字檔讀寫

struct與文字檔讀寫

7 則留言:

  1. #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <fstream>
    using namespace std;

    struct student
    {
    int id;
    string name;
    int java;
    int math;
    int cpp;
    bool sex;
    };

    void print(student X, ostream& ff)
    {
    ff << X.id << "," ;
    ff << X.name << ",";
    ff << X.java << ",";
    ff << X.math << ",";
    ff << X.cpp << ",";
    ff << X.sex << ",";
    ff << "\n";

    }
    void read(student&X, fstream& ff)
    {
    ff >> X.id;
    ff >> X.name;
    ff >> X.java;
    ff >> X.math;
    ff >> X.cpp;
    ff >> X.sex;
    }
    int main()
    {
    student Y[10];
    fstream ff, fout;
    ff.open("test.txt", ios::in);
    fout.open("ost.csv", ios::out);
    for (int i = 0; i < 10; i++)
    {
    read(Y[i], ff);
    print(Y[i], cout);
    print(Y[i],fout);
    }
    fout.close();
    ff.close();
    system("Pause");
    return 0;
    }

    回覆刪除
  2. #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <fstream>
    using namespace std;

    struct student
    {
    int id;
    string name;
    int java;
    int math;
    int cpp;
    bool sex;
    };

    void print(student X, ostream& ff)
    {
    ff << X.id << "," ;
    ff << X.name << ",";
    ff << X.java << ",";
    ff << X.math << ",";
    ff << X.cpp << ",";
    ff << X.sex << ",";
    ff << "\n";

    }
    void read(student&X, fstream& ff)
    {
    ff >> X.id;
    ff >> X.name;
    ff >> X.java;
    ff >> X.math;
    ff >> X.cpp;
    ff >> X.sex;
    }
    int main()
    {
    student Y[10];
    fstream ff, fout;
    ff.open("test.txt", ios::in);
    fout.open("ost.csv", ios::out);
    for (int i = 0; i < 10; i++)
    {
    read(Y[i], ff);
    print(Y[i], cout);
    print(Y[i],fout);
    }
    fout.close();
    ff.close();
    system("Pause");
    return 0;
    }

    回覆刪除
  3. #include
    #include
    #include
    #include
    using namespace std;

    struct student
    {
    int id;
    string name;
    int java;
    int math;
    int cpp;
    bool sex;
    };

    void print(student X, ostream& ff)
    {
    ff << X.id << "," ;
    ff << X.name << ",";
    ff << X.java << ",";
    ff << X.math << ",";
    ff << X.cpp << ",";
    ff << X.sex << ",";
    ff << "\n";

    }
    void read(student&X, fstream& ff)
    {
    ff >> X.id;
    ff >> X.name;
    ff >> X.java;
    ff >> X.math;
    ff >> X.cpp;
    ff >> X.sex;
    }
    int main()
    {
    student Y[10];
    fstream ff, fout;
    ff.open("test.txt", ios::in);
    fout.open("ost.csv", ios::out);
    for (int i = 0; i < 10; i++)
    {
    read(Y[i], ff);
    print(Y[i], cout);
    print(Y[i],fout);
    }
    fout.close();
    ff.close();
    system("Pause");
    return 0;
    }

    回覆刪除
  4. #include
    #include
    #include
    #include
    using namespace std;

    struct student
    {
    int id;
    string name;
    int java;
    int math;
    int cpp;
    bool sex;
    };

    void print(student X, ostream& ff)
    {
    ff << X.id << "," ;
    ff << X.name << ",";
    ff << X.java << ",";
    ff << X.math << ",";
    ff << X.cpp << ",";
    ff << X.sex << ",";
    ff << "\n";

    }
    void read(student&X, fstream& ff)
    {
    ff >> X.id;
    ff >> X.name;
    ff >> X.java;
    ff >> X.math;
    ff >> X.cpp;
    ff >> X.sex;
    }
    int main()
    {
    student Y[10];
    fstream ff, fout;
    ff.open("test.txt", ios::in);
    fout.open("ost.csv", ios::out);
    for (int i = 0; i < 10; i++)
    {
    read(Y[i], ff);
    print(Y[i], cout);
    print(Y[i],fout);
    }
    fout.close();
    ff.close();
    system("Pause");
    return 0;
    }

    回覆刪除
  5. #include
    #include
    #include
    #include
    using namespace std;

    struct student
    {
    int id;
    string name;
    int java;
    int math;
    int cpp;
    bool sex;
    };

    void print(student X, ostream& ff)
    {
    ff << X.id << "," ;
    ff << X.name << ",";
    ff << X.java << ",";
    ff << X.math << ",";
    ff << X.cpp << ",";
    ff << X.sex << ",";
    ff << "\n";

    }
    void read(student&X, fstream& ff)
    {
    ff >> X.id;
    ff >> X.name;
    ff >> X.java;
    ff >> X.math;
    ff >> X.cpp;
    ff >> X.sex;
    }
    int main()
    {
    student Y[10];
    fstream ff, fout;
    ff.open("test.txt", ios::in);
    fout.open("ost.csv", ios::out);
    for (int i = 0; i < 10; i++)
    {
    read(Y[i], ff);
    print(Y[i], cout);
    print(Y[i],fout);
    }
    fout.close();
    ff.close();
    system("Pause");
    return 0;
    }

    回覆刪除
  6. #include
    #include
    #include
    #include
    using namespace std;

    struct student
    {
    int id;
    string name;
    int java;
    int math;
    int cpp;
    bool sex;
    };

    void print(student X, ostream& ff)
    {
    ff << X.id << "," ;
    ff << X.name << ",";
    ff << X.java << ",";
    ff << X.math << ",";
    ff << X.cpp << ",";
    ff << X.sex << ",";
    ff << "\n";

    }
    void read(student&X, fstream& ff)
    {
    ff >> X.id;
    ff >> X.name;
    ff >> X.java;
    ff >> X.math;
    ff >> X.cpp;
    ff >> X.sex;
    }
    int main()
    {
    student Y[10];
    fstream ff, fout;
    ff.open("test.txt", ios::in);
    fout.open("ost.csv", ios::out);
    for (int i = 0; i < 10; i++)
    {
    read(Y[i], ff);
    print(Y[i], cout);
    print(Y[i],fout);
    }
    fout.close();
    ff.close();
    system("Pause");
    return 0;
    }

    回覆刪除
  7. #include
    #include
    #include
    #include
    using namespace std;

    struct student
    {
    int id;
    string name;
    int java;
    int math;
    int cpp;
    bool sex;
    };

    void print(student X, ostream& ff)
    {
    ff << X.id << "," ;
    ff << X.name << ",";
    ff << X.java << ",";
    ff << X.math << ",";
    ff << X.cpp << ",";
    ff << X.sex << ",";
    ff << "\n";

    }
    void read(student&X, fstream& ff)
    {
    ff >> X.id;
    ff >> X.name;
    ff >> X.java;
    ff >> X.math;
    ff >> X.cpp;
    ff >> X.sex;
    }
    int main()
    {
    student Y[10];
    fstream ff, fout;
    ff.open("test.txt", ios::in);
    fout.open("ost.csv", ios::out);
    for (int i = 0; i < 10; i++)
    {
    read(Y[i], ff);
    print(Y[i], cout);
    print(Y[i],fout);
    }
    fout.close();
    ff.close();
    system("Pause");
    return 0;
    }

    回覆刪除

HTML 編輯器