美文网首页
第七章 文件和结构体 7.1 文件操作 137页

第七章 文件和结构体 7.1 文件操作 137页

作者: 壹顾倾城 | 来源:发表于2019-12-16 10:37 被阅读0次
/********************************
 * 程序来源:董老师一本通
 * 程序名称:第七章 文件和机构体 137页
 *  章      节:7.1 文件操作
 * 作       者:tiaya@qq.com
 * 运行测试:通过
 *******************************/
//#include <bits/stdc++.h>  //万能头文件,不建议使用
#include <iostream>
#include <fstream>    //ifsream   ofstream 

using namespace std;

//main() star
int main() {
    //code here
    ifstream fin("in.txt");     //定义输入文件 
    ofstream fout("out.txt");   //定义输出文件 
    
    int temp = 0, sum = 0;
    
    while(fin >> temp) {        //读取输入数据 
        sum += temp;
    }
    fout << "sum=" << sum << endl; //输出文件 
    
    fin.close();
    fout.close();
    return 0;
}

测试:
输入数据:
in.txt

1
3
6
9

输出数据:

sum=19

改标准输入和输出

/********************************
 * 程序名称:
 * 作    者:tiaya@qq.com
 * 运行测试:通过
 *******************************/
//#include <bits/stdc++.h>  //万能头文件,不建议使用
#include <iostream>
#include <fstream>    //ifsream   ofstream 

#define fin cin
#define fout cout

using namespace std;

//main() star
int main() {
    //code here
    //ifstream fin("in.txt");     //定义输入文件 
    //ofstream fout("out.txt");   //定义输出文件 
    
    int temp = 0, sum = 0;
    
    while(fin >> temp) {        //读取输入数据 
        sum += temp;
    }
    fout << "sum=" << sum << endl; //输出文件 
    
    //fin.close();
    //fout.close();
    return 0;
}

测试:

5
6
3
65
332
^Z
sum=411

--------------------------------
Process exited after 6.268 seconds with return value 0
请按任意键继续. . .

相关文章

网友评论

      本文标题:第七章 文件和结构体 7.1 文件操作 137页

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