简体   繁体   English

C ++ bash管道内存

[英]C++ bash pipe memory

I'm using what I heard is "bash pipe command" to run multiple input files through multiple modification programs. 我正在使用所听到的“打击管道命令”通过多个修改程序运行多个输入文件。 Example command: 示例命令:

cat input1.cpp input2.cpp input3.cpp | ./calc | ./rmv_comments | ./calc

With this command I first run 3 different files through a program that calculate characters combined in the files, 230 + 450 + 100 = 780 for example. 使用此命令,我首先通过一个程序运行3个不同的文件,该程序计算文件中合并的字符,例如230 + 450 + 100 = 780。 Once that is done, comments from all input files are removed. 完成后,将删除所有输入文件中的注释。 After all comments are removed I need to run the calculation program again to get the amount of reduction in characters. 删除所有注释后,我需要再次运行计算程序以获取减少的字符数。

My problem is that once I run the rmv_comments program between the calculations I no longer know how many characters I had originally. 我的问题是,一旦在rmv_comments计算之间运行rmv_comments程序,我将不再知道最初拥有多少个字符。 How or where can I store the amount of character originally? 我最初如何或在哪里存储字符数? Also, how can I cout or printf the amount of reduction without messing with the output that is redirected as an input to rmv_comments? 另外,如何在不干扰作为rmv_comments的输入重定向的输出的情况下减少或打印减少量?

calc.cpp : calc.cpp

int main()
{
    char c;
    int count;

    while(cin.get(c))
    {
        count++;

        cout << c;
    }

    // How do I store "OLDCOUNT" when this program (calc.cpp) run the first time?
    int reduction = (1 - count/OLDCOUNT) * 100;

    // How can I printf or cout the result ONLY to command line
    // NOT to output (input of rmv_comments)
    printf("Reduction: %d", reduction);

    return 0;
}

rmv_comments.cpp : rmv_comments.cpp

int main()
{
    string line;
    while(getline(cin, line))
    {
        char c;
        string tmp;
        for(int i=0; i<line.length(); i++)
        {
            if(line[i] == '/' && line[i+1] == '/')
                break;

            tmp += line[i];
        }
        cout << tmp << endl;
    }

    return 0;
}

How about using a file to store the value? 如何使用文件存储值?

If the doesn't exist, then you know it's a first run, you do your calculations and create the file and store whatever data you need to store. 如果不存在,那么您就知道它是第一次运行,您需要进行计算并创建文件,然后存储需要存储的所有数据。

If the file exist, it's the second run, and you load the data from the file and then remove the file. 如果文件存在,则是第二次运行,然后从文件中加载数据,然后删除文件。

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

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