简体   繁体   English

将输出定向到C ++中的.txt文件

[英]Directing output into a .txt file in C++

How do I direct the output of this code into a .txt file? 如何将此代码的输出定向到.txt文件?

#include <iostream>
#include <vector>
#include <string>
#include <fstream>

using namespace std;

int main() {

    using std::cin;
    using std::cout;
    using std::endl;

    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) // after puting in data input zero 
        {
            break;
        }
        else if (input != 0)
        {
            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 if ( counter == 100 ) 
    {
        std::cout << "The hits are equal to 100" ;
    }
}

Also, instead of a user having to input data, how can I get the program to read data from another .txt file? 另外,我不必让用户输入数据,而是如何让程序从另一个.txt文件读取数据? I understand you can do this all easily in the terminal; 我了解您可以在终端中轻松完成所有操作; however, I would like for the program to create the .txt file. 但是,我希望该程序创建.txt文件。

Also, how do I get the program to recognize certain numbers? 另外,如何使程序识别某些数字? I want it to output something like "there was twelve number -11s counted". 我希望它输出类似“计数了十二个-11”的信息。

Use std::ifstream to read input from a file, and std::ofstream to write output to a file. 使用std::ifstream从文件读取输入,并使用std::ofstream将输出写入文件。 For example: 例如:

#include <iostream>
#include <fstream>

int main()
{
    int sum = 0; 
    int counter = 0;

    std::ifstream in("hits.txt");
    if (in.is_open())
    {
        while (in >> input)
        {
            sum += input; 
            ++counter;
        }
    }
    else
    {
        std::ofstream out("hits.txt");
        int input;

        do
        {
            std::cout << "Please enter the hit data: "; 

            // after putting in data, input zero 
            if (!(std::cin >> input) || (input == 0))
                break; 

            out << input << " ";

            sum += input; 
            ++counter;
        }
        while (true);
    }

    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" << std::endl;
    }
    else if (counter > 100) 
    {
        std::cout << "The hits are greater than 100" << std::endl;
    }
    else
    {
        std::cout << "The hits are equal to 100" << std::endl;
    }

    return 0;
}

Also, how do I get the program to recognize certain numbers? 另外,如何使程序识别某些数字? I want it to output something like "there was twelve number -11s counted". 我希望它输出类似“计数了十二个-11”的信息。

You can use std:map for that, eg: 您可以为此使用std:map ,例如:

#include <iostream>
#include <fstream>
#include <map>

int main()
{
    int sum = 0; 
    int counter = 0;
    std::map<int, int> hits; // hit counter

    std::ifstream in("hits.txt");
    if (in.is_open())
    {
        while (in >> input)
        {
            hits[input]++;
            sum += input; 
            ++counter;
        }
    }
    else
    {
        std::ofstream out("hits.txt");
        int input;

        do
        {
            std::cout << "Please enter the hit data: "; 

            // after putting in data, input zero 
            if (!(std::cin >> input) || (input == 0))
                break; 

            out << input << " ";

            hits[input]++;
            sum += input; 
            ++counter;
        }
        while (true);
    }

    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" << std::endl;
    }
    else if (counter > 100) 
    {
        std::cout << "The hits are greater than 100" << std::endl;
    }
    else
    {
        std::cout << "The hits are equal to 100" << std::endl;
    }

    for (auto &p : hits)
    {
        if (p.second == 1)
            std::cout << "there was 1 number " << p.first << " counted" << std:::endl;
        else
            std::cout << "there were " << p.second << " number " << p.first << "'s counted" << std:::endl;
    }

    /* or, if you are not using C++11 or later:

    for (std::map<int, int>::iterator iter = hits.begin(); iter != hits.end(); ++iter)
    {
        std::map<int, int>::value_type &p = *iter;

        if (p.second == 1)
            std::cout << "there was 1 number " << p.first << " counted" << std:::endl;
        else
            std::cout << "there were " << p.second << " number " << p.first << "'s counted" << std:::endl;
    }
    */

    return 0;
}

Outputting data to a .txt file is easy indeed. 将数据输出到.txt文件确实很容易。 You already included , now you need to create an object from the type std::ofstream and use it to write your text into a file. 您已经包括了,现在您需要从std :: ofstream类型创建一个对象,并使用它来将文本写入文件。 I would create a function like this (above main): 我将创建一个这样的函数(在main之上):

#include <iostream>
#include <fstream>
#include <string>

void outputTextToFile (std::string p_text) {
//is created under your project filepath:
  std::ofstream file("nameoffile.txt", std::ios::app); //"app" = appending, instead of overwriting text
  file << "Writing this to a file.\n";
  file.close();
}

Afterwards you can call your function in the while loop with the string text you want, like this for example: 然后,您可以在while循环中使用所需的字符串文本来调用函数,例如:

outputTextToFile("test Text");

Reading text from a .txt file is very similar to this, I would suggest you look up this thread: Read file line by line 从.txt文件中读取文本与此非常相似,我建议您查找此线程: 逐行读取文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM