簡體   English   中英

有沒有辦法防止 C++ output 中的單詞被“刪減”?

[英]Is there a way to prevent words from getting 'cut' in C++ output?

我在 C++ 中編寫了這個簡單的程序來打印 .txt 文件的內容。

void printrules(){
    string rule_path = "myfiles/rulebook.txt";
    ifstream fin;
    fin.open(rule_path.c_str());

    if (fin.fail()){
        cout << "Sorry, there was an error in displaying the rules." <<endl;
    }

    string x;
    while (fin >> x)
        cout << x << " ";

    fin.close();
}

int main(){
    printrules;
}

但是我想不出一種方法來阻止這些詞在控制台上是 output 時被“剪切”。 例如:

圖片

在第一行中,“h”和“e”被分開了,第二行和第三行的最后一個詞也發生了同樣的事情。

有沒有辦法在不固定行長的情況下確保單詞保持完整?

有很多方法可以做到這一點,但這里有一個相當簡單和容易的方法。

#include <sstream>
#include <string>
#include <iostream>

void wrap(std::string const &input, size_t width, std::ostream &os, size_t indent = 0) {
    std::istringstream in(input);

    os << std::string(indent, ' ');
    size_t current = indent;
    std::string word;

    while (in >> word) {
        if (current + word.size() > width) {
            os << "\n" << std::string(indent, ' ');
            current = indent;
        }
        os << word << ' ';
        current += word.size() + 1;
    }
}

int main() {
    char *in = "There was a time when he would have embraced the change that was coming. In his youth he"
    " sought adventure and the unknown, but that had been years ago. He wished he could go back and learn"
    " to find the excitement that came with change but it was useless. That curiousity had long ago left"
    " him to where he had come to loathe anything that put him out of his comfort zone.";

    wrap(in, 72, std::cout, 0);
    return 0;
}

這隱含地假設您要刪除輸入字符串中可能存在的額外空格,因此如果(例如)您開始使用格式化為 60 列的文本並帶有 5 個字符的縮進:

     There was a time when he would have embraced the change
     that was coming. In his youth he sought adventure and
     the unknown, but that had been years ago. He wished he
     could go back and learn to find the excitement that
     came with change but it was useless. That curiousity
     had long ago left him to where he had come to loathe
     anything that put him out of his comfort zone.

...但是您要求它在 72 列中自動換行,沒有縮進,它會擺脫用於先前縮進的空白,所以它會像這樣出現:

There was a time when he would have embraced the change that was coming.
In his youth he sought adventure and the unknown, but that had been
years ago. He wished he could go back and learn to find the excitement
that came with change but it was useless. That curiousity had long ago
left him to where he had come to loathe anything that put him out of his
comfort zone.

另一種可能性,特別是如果您想將所有output 自動換行到特定的 stream,將使用我在另一個答案中發布的自動換行 streambuf。

我接受了上面接受的答案並對其進行了修改,以便字符串流能夠從原始輸入文本中檢測換行符。 這是代碼:

void wrap(std::string const &input, int width, std::ostream &os, size_t indent = 0) {std::istringstream in(input);
os << std::string(indent, ' ');
size_t current = indent;
std::string line;
std::string word;
string output = "";

while(getline(in,line))
{
    std::istringstream linein(line);
    while (linein >> word) {
        if (current + word.size() > width) {
            output += "\n" + std::string(indent, ' ');
            current = indent;
        }
        output +=  word + ' ';
        current += word.size() + 1;
    }
    output += "\n" + std::string(indent, ' ');
    current = indent;
}

os << output;

我是使用 stackoverflow 的新手,所以我希望格式不會太難看。

干杯!

暫無
暫無

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

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