使用流样式从文件中读取一行

发布于 2024-08-18 03:17:18 字数 369 浏览 2 评论 0原文

我有一个简单的文本文件,其中包含以下内容,

word1 word2

我需要在 C++ 应用程序中读取它的第一行。 以下代码有效,……

std::string result;
std::ifstream f( "file.txt" );
f >> result;

结果变量将等于“word1”。它应该等于“word1 word2”(文本文件的第一行) 是的,我知道,我可以使用 readline(f, result) 函数,但是有没有办法做到同样的事情,使用 >>风格。这可能会更漂亮。 可能,一些我不知道的操纵器在这里会有用吗?

I have a simple text file, that has following content

word1 word2

I need to read it's first line in my C++ application.
Following code works, ...

std::string result;
std::ifstream f( "file.txt" );
f >> result;

... but result variable will be equal to "word1". It should be equal to "word1 word2" (first line of text file)
Yes, i know, that i can use readline(f, result) function, but is there a way to do the same, using >> style. This could be much more pretty.
Possible, some manipulators, i don't know about, will be useful here ?

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

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

发布评论

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

评论(3

眼眸 2024-08-25 03:17:18

是的,定义一个线类并定义运算符>>对于这堂课。

#include <string>
#include <fstream>
#include <iostream>


struct Line
{
    std::string line;

    // Add an operator to convert into a string.
    // This allows you to use an object of type line anywhere that a std::string
    // could be used (unless the constructor is marked explicit).
    // This method basically converts the line into a string.
    operator std::string() {return line;}
};

std::istream& operator>>(std::istream& str,Line& line)
{
    return std::getline(str,line.line);
}
std::ostream& operator<<(std::ostream& str,Line const& line)
{
    return str << line.line;
}

void printLine(std::string const& line)
{
    std::cout << "Print Srting: " << line << "\n";
}

int main()
{
    Line    aLine;
    std::ifstream f( "file.txt" );
    f >> aLine;

    std::cout << "Line: " << aLine << "\n";
   printLine(aLine);
}

Yes define a line class and define the operator >> for this class.

#include <string>
#include <fstream>
#include <iostream>


struct Line
{
    std::string line;

    // Add an operator to convert into a string.
    // This allows you to use an object of type line anywhere that a std::string
    // could be used (unless the constructor is marked explicit).
    // This method basically converts the line into a string.
    operator std::string() {return line;}
};

std::istream& operator>>(std::istream& str,Line& line)
{
    return std::getline(str,line.line);
}
std::ostream& operator<<(std::ostream& str,Line const& line)
{
    return str << line.line;
}

void printLine(std::string const& line)
{
    std::cout << "Print Srting: " << line << "\n";
}

int main()
{
    Line    aLine;
    std::ifstream f( "file.txt" );
    f >> aLine;

    std::cout << "Line: " << aLine << "\n";
   printLine(aLine);
}
转瞬即逝 2024-08-25 03:17:18

不,没有。使用 getline(f, result) 读取一行。

No, there isn't. Use getline(f, result) to read a line.

时光倒影 2024-08-25 03:17:18

您可以创建一个仅将换行符作为空白的本地文件,但这将是一个令人困惑的黑客行为。 这里是一个使用逗号进行此操作的示例。

You can create a local that only has newlines as whitespace, but that would be a confusing hack. Here is an example doing just that with commas.

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