簡體   English   中英

C ++ bash管道內存

[英]C++ bash pipe memory

我正在使用所聽到的“打擊管道命令”通過多個修改程序運行多個輸入文件。 示例命令:

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

使用此命令,我首先通過一個程序運行3個不同的文件,該程序計算文件中合並的字符,例如230 + 450 + 100 = 780。 完成后,將刪除所有輸入文件中的注釋。 刪除所有注釋后,我需要再次運行計算程序以獲取減少的字符數。

我的問題是,一旦在rmv_comments計算之間運行rmv_comments程序,我將不再知道最初擁有多少個字符。 我最初如何或在哪里存儲字符數? 另外,如何在不干擾作為rmv_comments的輸入重定向的輸出的情況下減少或打印減少量?

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

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;
}

如何使用文件存儲值?

如果不存在,那么您就知道它是第一次運行,您需要進行計算並創建文件,然后存儲需要存儲的所有數據。

如果文件存在,則是第二次運行,然后從文件中加載數據,然后刪除文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM