2012-11-05 1 views
2

문자열로 변환 된 전화 조회 프로그램을 파일로 스트리밍하려고 시도 중입니다. 뭔가 빠졌어.하지만 막혔어. ofstream 프로세스에서 어떤 멤버를 사용할 수 있습니까? 이게 효과가 있니?C++ 파일에서 벡터로 읽기

ofstream& process (ofstream &os, vector<PersonInfo> people) 
{ 
    // for each entry in people 
    for (vector<PersonInfo>::const_iterator entry = people.begin(); 
       entry != people.end(); ++entry) {  
     ofstream formatted, badNums; // objects created on each loop 

     // for each number 
     for (vector<string>::const_iterator nums = entry->phones.begin(); 
       nums != entry->phones.end(); ++nums) { 
      if (!valid(*nums)) {   
       badNums << " " << *nums; // string in badNums 
      } else       
       // ``writes'' to formatted's string 
       formatted << " " << format(*nums); 
     } 
     if (badNums.empty())  // there were no bad numbers 
      os << entry->name << " " // print the name 
       << formatted.str() << endl; // and reformatted numbers 
     else     // otherwise, print the name and bad numbers 
      cerr << "input error: " << entry->name 
       << " invalid number(s) " << badNums.str() << endl; 
    } 

    return os; 
} 
+3

스트림을 파일과 연결하지 않습니다. 'open()'을 호출하고 파일 이름 (또는 적절한 생성자)를 전달해야합니다. 그리고 스트림에는'empty()'라는 멤버 함수가 없다. – jrok

답변

4

첫째, 당신은 ofstream, 지점에서 해당 파일 을 개방하고 제외 (인스턴스 작성)를 원하지 않는다. 출력 스트림 인터페이스는 이고, 정의는 std::ostream입니다. std::ofstreamstd::ostringstream (출력은 std::string이 될 수 있음)과 대부분 응용 프로그램에서 비롯되며 로컬 프로그래머가 작성한 몇 가지 응용 프로그램과 마찬가지로이 값에서 파생됩니다. (나는 문제를 정확하게 이해 한 경우) 귀하의 경우에는, 당신이 원하는 무엇인가 :

std::ostream& process(std::ostream& os, 
         std::vector<PersonInfo> const& people) 
    // Note the use of a const reference above. No point 
    // in copying the entire vector if you're not going to 
    // modify it. 
{ 
    for (std::vector<PersonInfo>::const_iterator entry = people.begin(); 
      entry != people.end(); 
      ++ entry) { 
     std::ostringstream formatted; 
     std::ostringstream badNums; 
     // ... 
     if (badNums.str().empty()) { 
      os << ... << formatted.str() << std::endl; 
     } else { 
      os << ... << badNums.str() << std::endl; 
     } 
    } 
    return os; 
} 

참고 다른 유형 : std::ostream 형식의 출력을 독립적으로 대상 유형의 . std::ofstream에서 파생되어 파일을 대상으로 제공합니다. std::ostringstream에서 파생되며 은 대상 유형으로 std::string을 제공합니다. std::ostreamstd::streambuf*을 인수로 사용하며 대상에 유형을 제공합니다.

0

이 기능의 내부 부품에는 ofstream을 사용할 필요가 없습니다. 사실 전혀 스트림을 사용하지 마십시오 std::string는 할 것 :

ofstream& process (ofstream &os, vector<PersonInfo> people) 
{ 
    // for each entry in people 
    for (vector<PersonInfo>::const_iterator entry = people.begin(); 
       entry != people.end(); ++entry) {  
     string formatted, badNums; // objects created on each loop 

     // for each number 
     for (vector<string>::const_iterator nums = entry->phones.begin(); 
       nums != entry->phones.end(); ++nums) { 
      if (!valid(*nums)) {   
       badNums += " " + *nums; // string in badNums 
      } else       
       // ``writes'' to formatted's string 
       formatted += " " + format(*nums); 
     } 
     if (badNums.empty())  // there were no bad numbers 
      os << entry->name << " " // print the name 
       << formatted << endl; // and reformatted numbers 
     else     // otherwise, print the name and bad numbers 
      cerr << "input error: " << entry->name 
       << " invalid number(s) " << badNums << endl; 
    } 

    return os; 
} 
0

당신은 ostream에있는 파일을 연결하지, 그래서 컴파일러는 당신이 그것으로 쓰기 데이터로 무엇을 해야할지하지 않습니다 .

ofstream& process (ofstream &os, vector<PersonInfo> people) 
{ 
    os.open("Data.txt"); //open file to be used 
    if(!os.is_open()) 
     std::cerr << "Error opening file!\n"; 
    //rest of code goes here 
} 

EDIT : 프로그램을 다시 읽은 후 ofstream을 잘못 사용하는 것으로 나타났습니다. Ofstream은 FILES를 작성하고 작성하기위한 것입니다. 프로그램에는 구문과 논리적 오류가 많이 포함되어 있습니다. 더 많은 정보는 here에 있습니다.