有没有一种方法可以防止单词在C ++输出中被“剪切”?

时间:2020-05-03 05:54:51

标签: c++ c++11 ifstream cout

我已经用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;
}

但是我想不出一种方法来阻止单词在输出到控制台时被“剪切”。例如:

image

在第一行中,“ h”和“ e”分开了,第二行和第三行中的最后一个单词也发生了同样的事情。

有没有一种方法可以确保单词在不固定行长的情况下保持完整?

2 个答案:

答案 0 :(得分:1)

有很多方法可以做到这一点,但这是一种非常简单容易的方法。

#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.

另一种可能性,尤其是如果您想将输出 all 全部包装到特定的流中,则可以使用我在another answer中发布的自动包装流包。

答案 1 :(得分:0)

我接受了上面接受的答案并对其进行了修改,以便字符串流能够从原始输入文本中检测到换行符。代码如下:

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,所以我希望格式不会太难看。

干杯!

相关问题