zoukankan      html  css  js  c++  java
  • C++之文件输入输出

    在这里遇见不少的问题,其中的路径问题就是在windows中,\转义字符才能准确的表示路径

    #include <iostream>
    #include <fstream>
    #include <algorithm>
    
    #define max 105
    using namespace std;
    
    struct boy{
        char name[20];
        int china;
        int math;
        int english;
    
        int sum;
        boy(){
            china = math = english = 0;
        }
    
    }stu[max];
    
    bool operator <(const boy &a,const boy &b)
     {
        return a.sum<b.sum;
     }
    int main()
    {
        ///创建了读取和写出数据的两个对象
        ifstream input;
        ofstream output;
    
    if(input.fail()){
            cout<<"文件不存在"<<endl;
        }
        else{
        ///打开文件
        input.open("f:\file1.txt");
        ///Create a file
        output.open("f:\file2.txt");
        cout<<"文件设置成功"<<endl;
        int i = 0;
        while(!input.eof()){
            input >> stu[i].name >> stu[i].china >> stu[i].math >> stu[i].english;
            stu[i].sum = stu[i].china+stu[i].english+stu[i].math;
    
    
            cout<< stu[i].name<<"  "<<stu[i].china<<"  "<<stu[i].math<<"  "<<stu[i].english<<"  "<<stu[i].sum<<endl;
            i++;
            cout<<"读取第"<<i<<"条数据成功"<<endl;
        }
        cout<<"文件读取成功"<<endl;
        ///关闭文件
        input.close();
    
        sort(stu,stu+i);
        for(int j = 0;j < i;j++){
            output<< stu[j].name<<"  "<<stu[j].china<<"  "<<stu[j].math<<"  "<<stu[j].english<<"  "<<stu[j].sum<<endl;
        }
    cout<<"文件写入成功"<<endl;
        output.close();
        }
        return 0;
    }

    zhangjie 11 11 13
    dingwei 22 43 43
    kaka 32 32 34
    adfa 32 42 44
    zheng 23 23 23

    数量一共为30到100之间的数量
    文件名是file1.txt
    name chinese math English
    章杰    33    44    55

    将上述文件写为file2.txt文件,通过总分进行一个排序,
    并将它以
    name Chinese math English sum 的形式写出

    我要坚持一年,一年后的成功才是我想要的。
  • 相关阅读:
    约合¥1720 LG法国称G Watch将于6月开卖
    c语言中的位移位操作
    兼容的动态加载JS【原】
    Oracle 10g AND Oracle 11g手工建库案例--Oracle 10g
    面向对象思想----不看懊悔!
    学习设计模式--观察者模式(C++)
    使用POI 导入excel
    机器学习 F1-Score, recall, precision
    阿里无线11.11:手机淘宝移动端接入网关基础架构演进之路
    ICE 迁移64位安装问题
  • 原文地址:https://www.cnblogs.com/tianxia2s/p/4498470.html
Copyright © 2011-2022 走看看