【问题标题】:How to getline() twice on same line in C++?如何在 C++ 中的同一行上两次 getline()?
【发布时间】:2019-11-17 05:06:01
【问题描述】:

我有一个文件,我想存储一行的第一部分,直到到达“,”,然后将该行的剩余部分存储到另一个变量中。目前,它跳过了一半的行。例如这是我文件的三行:

021200725340,Scotch Removable Clear Mounting Squares - 35 Ct
041520035646,Careone Family Comb Set - 8 Ct
204040000000,Plums Black

我的代码只存储了一半的行:

while(getline(file, upc)){ 
    getline(file, upc, ',');        
    getline(file, description); 
}

【问题讨论】:

  • 你为什么不 getline() 不带分隔符并在 ',' 上自己拆分?

标签: c++ getline


【解决方案1】:

我的代码只存储了一半的行:

while 语句的条件内调用getline 负责。

将您的代码更改为:

while( getline(file, upc, ',') && getline(file, description) )
{
   // Use the data.
}

【讨论】:

    猜你喜欢
    • 2021-12-05
    • 2013-10-14
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多