2014-04-11 4 views
1

문제 :istringstream 문제

내가 "txt 파일"아래 아래 필요로의 정보 라인 텍스트 파일이 있습니다. 나는 과제를 완료 할 수 있도록 항목을 매핑하려고 시도하고 있습니다. 매핑 할 때 나는 istringstream을 사용하고 있습니다. 내 문제는 하나의 문자열에 저장하려는 항목에 여러 단어가있을 때 작동하도록하는 데 있습니다. 예를 들어 "Unsweetened Applesauce"는 하나의 문자열 (item.setNewItem)이되기를 원합니다. 어떤 도움도 정말로 감사 할 것입니다, 그리고 저는 현재의 학생이기 때문에, 저를 위해서 모든 어리 석음이 정말 감사 할 것입니다. =)

txt 파일 :

1 컵 설탕 | 1 컵 Unsweetened Applesauce | 칼로리

코드 :

void mapList(ifstream &foodReplace, map<string, Substitutions> &subs) 
{ 
    string line; 
    while (getline (foodReplace, line)); 
    { 
     Substitutions item; 
     istringstream readLine(line); 

     readLine << item.setOldAmount 
       << item.setOldMeasurement 
       << item.setOldItem 
       << item.setNewAmount 
       << item.setNewMeasurement 
       << item.setNewItem; 

     subs.insert(pair<string, Substitutions>(item.getOldItem, item)); 
    } 

} 
+0

논리적 조각 사이에서 문자열을 분리 할 수 ​​있도록 단어 사이의 구분 기호를 사용하도록 입력을 변경하십시오. 'Food Unsweetened Applesauce endFood'와 같은 무언가 가난한 사람의 XML입니다 .-) – AndyG

+0

XML은 모든 사람들이 알고 있듯이 가난한 사람의 CSV입니다. – Deduplicator

답변

0

당신은 단지 구분 기호 지정의 getline에 세 번째 매개 변수를 제공 할 수 있습니다 :

http://www.cplusplus.com/reference/string/string/getline/

을 한 다음에 제 1 및 제 2 필드를 읽을 수 있습니다 ''로, 세 번째 필드는 '|'로 설정하십시오.

통화가 읽을 것 : 정말 모든 사람의 도움을 주셔서 감사합니다

void mapList(ifstream &foodReplace, map<string, Substitutions> &subs) 
{ 
    string line; 
    while (getline (foodReplace, line)); 
    { 
     Substitutions item; 
     istringstream readLine(line); 

     getline(readLine, item.setOldAmount, ' '); //you may need to read this in to a temp string if setOldAmount is an int 
     getline(readLine, item.setOldMeasurement, ' '); 
     getline(readLine, item.setOldItem, '|'); 

     getline(readLine, item.setNewAmount, ' '); //you may need to read this in to a temp string if setNewAmount is an int 
     getline(readLine, item.setNewMeasurement, ' '); 
     getline(readLine, item.setNewItem, '|'); 

     subs.insert(pair<string, Substitutions>(item.getOldItem, item)); 
    } 

} 
0

, 그것은 내가 동일한 코드를 사용하지 않은 경우에도 필요한 경우 나에 도착 도움이 되었는가, 아래의 솔루션입니다 I 모두와 다시 함께 갔다. =)

//function to populate map and update object info 
void mapList(fstream &foodReplace, map<string, Substitutions> &subs) 
{ 
    string line; 
    while (getline (foodReplace, line)) //gets the line and saves it to line 
    { 
     Substitutions item; 
     istringstream readLine(line); //reads it into readLine 

     //declaring variables 
     double amount; 
     string unit; 
     string ingredient; 
     string benefit; 

     //gets old ingredient and saves in object 
     getIngredient(readLine, amount, unit, ingredient); 
     item.setOldAmount(amount); 
     item.setOldMeasurement(unit); 
     item.setOldItem(ingredient); 

     //gets new ingredient and saves to object 
     getIngredient(readLine, amount, unit, ingredient); 
     item.setNewAmount(amount); 
     item.setNewMeasurement(unit); 
     item.setNewItem(ingredient); 

     //reads in last piece and saves in object 
     readLine >> benefit; 
     item.setBenefit(benefit); 

     //inserts object into map 
     subs.insert(pair<string, Substitutions>(item.getOldItem(), item)); 
    } 
} 

//function to extract amount-units-ingredient 
void getIngredient(istringstream &stream, double &amount, string &unit, string &ingredient) 
{ 
    //resetting variables 
    amount = 0; 
    unit = ""; 
    ingredient = ""; 
    string temp; 

    //setting variables 
    stream >> amount; 
    stream >> unit; 
    stream >> temp; 

    //read until delimiter is hit 
    while (temp != "|") 
    { 
     ingredient += temp + " "; 
     stream >> temp; 
    } 

    //removes the space at the end of the ingredient 
    ingredient = ingredient.substr(0, ingredient.length() - 1); 
}