windows与linux std :: getline string

时间:2013-02-01 03:19:34

标签: linux xml-parsing g++ getline

我有一个xml文件,我想从

中提取一些特定字段

示例测试文件

<ScheduleTask ID="ZmwnBtczlnYOBLyK" Code="000-000.0.0.0" Name="SETTLEMENT.BRIDGE" ParentSummaryTaskID="">
    <Quantities>
        <STQuantity MethodID="416" RecipeID=""/>
    </Quantities>
    <Timings>
        <Timing LocationID="$$$$$&gt;C1&gt;NB" CompletionRate="1">
            <Planned Begin="2012-03-08T07:14:57" End="2012-03-28T07:14:57"/>
            <Actual Begin="2012-03-31T06:00:00" End="2012-05-01T14:00:00"/>
        </Timing>
        <Timing LocationID="$$$$$&gt;C1&gt;SB" CompletionRate="0">
            <Planned Begin="2012-12-04T06:07:29" End="2012-12-24T06:07:29"/>
            <Forecast Begin="2013-04-18T09:16:37" End="2013-06-04T12:06:02"/>
        </Timing>
    </Timings>
    </ScheduleTask>

所以,我有这个函数用/ ScheduleTask查找一行(包括&lt;)

//xml file into ontology
list<stack<string> > addWordsFromFile(string filename, int changeLevel)
{
    ifstream ontology;
    ontology.open(filename.c_str()); 
string ontTemp, line;

list<stack<string> > control_list;

while (true) {
    getline(ontology, line); //read line
    if (ontology.fail()) break; //boilerplate check for error
    line.erase(remove(line.begin(), line.end(), '\t'), line.end()); //remove tabs

    if(line == "</ScheduleTasks>") break; //check for end of document

        if(line == "</ScheduleTask>") {
        ontTemp.clear(); //clear memory
    }

    //look for activity
    if(line.substr(1, 15) == "ScheduleTask ID") {
        int i = 41;
        while (line[++i] != '"') {ontTemp += line[i]; }
    }

    if (ontTemp != "" ) {//ready to add
        stack<string> tempMem;

        tempMem.push(ontTemp);

        control_list.push_back(tempMem);
    }
}
ontology.close();
return control_list;
}

在Windows中,这个函数工作正常并且找到了/ScheduleTask - 在Linux中找不到它,尽管其他字段在if(line.substr(1, 15) == "ScheduleTask ID")

中找不到

使用VisualStudio 2008和g ++编译

我的问题:1)为什么这在Linux中不起作用?2)它是如何工作的?

1 个答案:

答案 0 :(得分:1)

这归结为行尾的定义。 Windows EOL为\r\n,而Linux EOL为\n。这一行

</ScheduleTask>\r\n

getline返回

Windows上为

</ScheduleTask>,但Linux中为</ScheduleTask>\r