c++ - 使用 getfile 跳过一行

标签 c++

对于我的程序,我们应该读取一个文本文件并将其打印到屏幕上。在文本文件中,消息可能如下所示:

#email
mailservice
birdman
Intro 
Hello Birdman! Welcome!
#email
Frank
birdman
Hello
Hello Birdman, how are you?

我需要跳过#email 以便打印出来:

From: mailservice
To: birdman
Subject: Intro 
Message: Birdman! Welcome!

From: Frank
To: birdman
Subject: Hello
Message:Hello Birdman, how are you?

这是我的代码:

std::string filename1 = GetInboxFile(username);
std::ifstream fin(filename1);
std::string word;
std::cout << "\n";

std::string formatted = FormatEmailString("From: ", "To: ", "Subject: ", "Message: ");
//FormatEmailString is a function

std::cout << formatted;

while (fin.eof() == false)
{

    std::string line = GetLine(fin);

    if (fin.eof() == false)
    {

        std::cout << line << std::endl;

    }//END IF STATEMENT

}//END WHILE LOOP

std::cout << std::endl;
fin.close();

最佳答案

while (fin)
{
    std::string line = GetLine(fin);

    if (fin) // @DietmarKühl: checking if the input was successful 
    {

        // if (line.find("#email") != std::string::npos) 
        if(line == "#email\n") 
        {
             line = "\n"; // you want a empty line instead 
             // continue; // you could use continue to simply skip it
        }

        std::cout << line << std::endl;    
    }
}

关于c++ - 使用 getfile 跳过一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34103309/

相关文章:

c++ - 将 undefined reference 错误限制为仅直接依赖

c++ - C++ 上的 std::map 问题

c++ - libiconv - iconv_open() 默认行为?

c++ - 修复(锁定)std::vector 的大小

c++ - std::chrono::system_clock.now().time_since_epoch().count() 的值是否单调递增?

C++ - 为什么删除后将对象设置为空?

c++ - 将结构从 C 转换为 C++ 类

c++ - 在 C++ 中将 UID 设置为 root 以外的用户

c++ - Clang 为使用的类型别名发出 "unused type alias"警告

Java套接字与Qt的连接