You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++程序输出定向至.txt文件、读取文件及特定数字统计问题

嘿,我来帮你一步步解决这三个问题,直接上干货:

问题1:把程序输出定向到.txt文件

咱们可以通过C++的ofstream类来实现输出到文件,有两种常用方式:一种是直接用ofstream对象输出内容,另一种是重定向标准输出cout到文件。下面是修改后的代码,我用了重定向的方式,这样你原来的cout语句不用改太多:

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

int main() {
    using std::cin;
    using std::cout;
    using std::endl;
    using std::ofstream;

    // 创建输出文件流,指定输出文件名
    ofstream outFile("output.txt");
    if (!outFile) { // 检查文件是否成功打开
        cerr << "无法创建输出文件!" << endl;
        return 1;
    }

    // 把cout重定向到outFile
    streambuf* originalCout = cout.rdbuf();
    cout.rdbuf(outFile.rdbuf());

    int input=1;
    int sum=0;
    int counter=1;
    while (input != 0) {
        std::cout << "Please enter the hit data: ";
        std::cin >> input;
        if (input == 0) {
            break;
        } else {
            sum += input;
            counter++;
        }
    }
    counter = counter - 1 ;
    std::cout << "Sum of hits entered: " << sum << endl ;
    std::cout << "Number of hits entered: " << counter << endl ;
    if ( counter < 100 ) {
        std::cout << "The hits are less than 100" ;
    } else if ( counter > 100 ) {
        std::cout << "The hits are greater than 100" ;
    } else {
        std::cout << "The hits are equal to 100" ;
    }

    // 恢复cout的原始输出流(可选,如果你之后还要用cout输出到控制台的话)
    cout.rdbuf(originalCout);
    outFile.close(); // 关闭文件流
    return 0;
}

解释:这里我们创建了output.txt文件,把cout的输出重定向到这个文件里,所有原来用cout打印的内容都会写到文件里,而不是控制台。记得要检查文件是否成功创建,避免出错。

问题2:从.txt读取数据,自动创建输出.txt

这次我们用ifstream读取输入文件(比如input.txt),同时用ofstream创建输出文件。不需要手动输入了,程序会自动从输入文件读数据,直到读到0为止:

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

int main() {
    using std::cout;
    using std::endl;
    using std::ifstream;
    using std::ofstream;

    // 打开输入文件
    ifstream inFile("input.txt");
    if (!inFile) {
        cerr << "无法打开输入文件!" << endl;
        return 1;
    }

    // 创建并打开输出文件
    ofstream outFile("output.txt");
    if (!outFile) {
        cerr << "无法创建输出文件!" << endl;
        inFile.close();
        return 1;
    }

    int input=1;
    int sum=0;
    int counter=0; // 这里调整初始值,因为从文件读不需要初始的1了
    while (inFile >> input) { // 从文件读取数据,直到文件结束或读到0
        if (input == 0) {
            break;
        }
        sum += input;
        counter++;
    }

    // 写入输出文件
    outFile << "Sum of hits entered: " << sum << endl ;
    outFile << "Number of hits entered: " << counter << endl ;
    if ( counter < 100 ) {
        outFile << "The hits are less than 100" << endl;
    } else if ( counter > 100 ) {
        outFile << "The hits are greater than 100" << endl;
    } else {
        outFile << "The hits are equal to 100" << endl;
    }

    // 关闭文件流
    inFile.close();
    outFile.close();

    cout << "处理完成!结果已写入output.txt" << endl;
    return 0;
}

解释:

  • 你需要提前准备好input.txt,里面每行或者用空格分隔放你的数据,最后加个0作为结束标志。
  • 程序会自动创建output.txt并写入结果,同时控制台会提示处理完成。
  • 注意调整了counter的初始值,因为不需要手动输入的那个初始计数了。
问题3:识别特定数字并统计输出(以-11为例)

我们只需要添加一个计数器,专门统计目标数字(比如-11)的出现次数,每次读取数据时判断是否等于目标数字,然后更新计数器,最后输出统计结果:

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

int main() {
    using std::cout;
    using std::endl;
    using std::ifstream;
    using std::ofstream;

    ifstream inFile("input.txt");
    if (!inFile) {
        cerr << "无法打开输入文件!" << endl;
        return 1;
    }

    ofstream outFile("output.txt");
    if (!outFile) {
        cerr << "无法创建输出文件!" << endl;
        inFile.close();
        return 1;
    }

    const int TARGET_NUM = -11; // 定义要统计的特定数字
    int input=1;
    int sum=0;
    int counter=0;
    int target_count = 0; // 统计目标数字的次数

    while (inFile >> input) {
        if (input == 0) {
            break;
        }
        sum += input;
        counter++;
        if (input == TARGET_NUM) { // 判断是否是目标数字
            target_count++;
        }
    }

    outFile << "Sum of hits entered: " << sum << endl ;
    outFile << "Number of hits entered: " << counter << endl ;
    if ( counter < 100 ) {
        outFile << "The hits are less than 100" << endl;
    } else if ( counter > 100 ) {
        outFile << "The hits are greater than 100" << endl;
    } else {
        outFile << "The hits are equal to 100" << endl;
    }
    // 输出目标数字的统计结果
    outFile << "共统计到" << target_count << "个" << TARGET_NUM << endl;

    inFile.close();
    outFile.close();

    cout << "处理完成!结果已写入output.txt" << endl;
    return 0;
}

解释:

  • 用const int TARGET_NUM = -11定义要统计的数字,方便你以后修改成其他数字。
  • 添加了target_count计数器,每次读到目标数字就加1。
  • 最后在输出文件里加上统计结果的语句,格式就是你要的“共统计到X个Y”。

内容的提问来源于stack exchange,提问作者conDon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:24:30