从 iostream 读取

发布于 2024-12-09 22:09:03 字数 516 浏览 1 评论 0原文

也许我遗漏了一些东西,但是我很难找到有关如何从 iostream (std::iostream& stream) 读取的任何信息。有没有办法可以将其转换为字符串或类似的?

为了澄清这一点(例如,我基本上想做的事情):

std::stringstream ss("Maybe I'm missing something \n but I'm having a lot of trouble finding any information on how to how to read from an iostream.");
readStream(ss);

void readStream(std::iostream& stream)
{
    std::string out;
    stream >> out;
    // Do some stuff with the string
}

这似乎有效,但 out 将等于“Maybe”而不是完整的字符串。

Maybe I'm missing something, but I'm having a lot of trouble finding any information on how to how to read from an iostream (std::iostream& stream). Is there a way I can convert it to a string or similar?

For clarification this is (what I'm basically trying to do, for example):

std::stringstream ss("Maybe I'm missing something \n but I'm having a lot of trouble finding any information on how to how to read from an iostream.");
readStream(ss);

void readStream(std::iostream& stream)
{
    std::string out;
    stream >> out;
    // Do some stuff with the string
}

This seems to work, but out will be equal to "Maybe" rather than the full string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

葬心 2024-12-16 22:09:03

从 iostream 读取数据的方式与使用 cin 时的方式相同。

stream >> varName;

是的,疯狂的语法,但这就是流的制造者决定做的。

如果您读取字符串,您还可以使用 get 和 getline。 Get 将获取下一个字符或指定的字符缓冲区,而 getline 将转到下一个换行符。

getline(stringName);

您可以在这里阅读更多相关内容:http://cplusplus.com/reference/iostream/iostream/

You read from an iostream the same way you would if you were using cin.

stream >> varName;

Crazy syntax yes, but that's what the makers of streams decided to do.

You can also use get and getline if your reading to strings. Get will get the next character or a specified buffer of characters, and getline will go to the next newline.

getline(stringName);

You can read more on this here: http://cplusplus.com/reference/iostream/iostream/

梦境 2024-12-16 22:09:03

流会自动转换为它们要转换到的类型。

using namespace std;
int number;
double fraction;
string world;
stream >> number >> fraction >> world;

当转移到字符串时,它会读取到第一个单词分隔符,您可能希望使用 std::获取线路

using namespace std;
string line;
getline(stream,line);

Streams converts automatically for the type they are shifting to.

using namespace std;
int number;
double fraction;
string world;
stream >> number >> fraction >> world;

When shifting to a string, it reads until the first word delimiter, you may wish to use std::getline.

using namespace std;
string line;
getline(stream,line);
小忆控 2024-12-16 22:09:03

也许您想阅读整行。在这种情况下,您必须使用 std::getline,因此具有:

void readStream(std::iostream& stream)
{
    std::string out;

    // while getting lines
    while(std::getline(stream, out))
    {
        // Do some stuff with each line
    }
}

您还可以通过将其作为第三个参数传递给 std::getline 来选择行分隔符。

Maybe you want to read whole lines. In this case you have to use std::getline, thus having:

void readStream(std::iostream& stream)
{
    std::string out;

    // while getting lines
    while(std::getline(stream, out))
    {
        // Do some stuff with each line
    }
}

You can also choose a line delimiter character, by passing it to std::getline as a third parameter.

傲影 2024-12-16 22:09:03

流运算符>>用于读取格式化的空白分隔文本。

int  val1;
stream >> val1;  // reads a space separated int

float val2;
stream >> val2;  // reads a space separated float

std::string val3;
stream >> val3;  // reads a space separated word.

不幸的是 std::string (和 C-Strings)不是对称的(输入/输出不以相同的方式工作(与其他基本类型不同))。当您写入它们时,它们会写入完整的字符串(直到 C 字符串的空终止符“\0”)。

If you want to read a whole line of text use std::getline()

std::string line;
std::getline(stream, line);

但与大多数语言一样,您可以循环读取流,直到完成。

std::string word;
while(stream >> word)
{
     // Reads one word at a time until the EOF.
     std::cout << "Got a word (" << word << ")\n";
}

或者一次一行一行地做同样的事情:

std::string line;
while(std::getline(stream, line))
{
     // Reads one word at a time until the EOF.
     std::cout << "Got a word (" << word << ")\n";
}

注 1:我提到上面用空格分隔。空白包括空格/制表符,最重要的是换行符,因此使用运算符>>上面它将一次读取一个单词,直到文件末尾,但忽略新行。

注2:运算符>>应该用于格式化文本。因此,它的第一个操作是删除前缀空白字符。在第一个非空白文本上,根据输入类型解析输入,并在与该类型不匹配的第一个字符处停止(这包括空白)。

The stream operator >> is used to read formatted white space separated text.

int  val1;
stream >> val1;  // reads a space separated int

float val2;
stream >> val2;  // reads a space separated float

std::string val3;
stream >> val3;  // reads a space separated word.

Unfortunately std::string (and C-Strings) are not symmetric (input/output do not work in the same way (unlike the other fundamental types)). When you write them they write the full string (up to the null terminator, '\0', of the C-string).

If you want to read a whole line of text use std::getline()

std::string line;
std::getline(stream, line);

But like most languages, you can loop reading the stream until it is finished.

std::string word;
while(stream >> word)
{
     // Reads one word at a time until the EOF.
     std::cout << "Got a word (" << word << ")\n";
}

Or the same thing one line at a time:

std::string line;
while(std::getline(stream, line))
{
     // Reads one word at a time until the EOF.
     std::cout << "Got a word (" << word << ")\n";
}

Note 1: I mentioned white space separated above. White space includes space/tab and most importantly new line so using the operator >> above it will read one word at a time until the end of file, but ignore new line.

Note 2: The operator >> is supposed to be used on formatted text. Thus its first action is to drop prefix white space characters. On the first non white space text, parse the input as appropriate for the input type and stop on the first character that does not match that type (this includes white space).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文