2012-02-28 5 views
0

찾은 정보의 대부분은 숫자를 기반으로하지만 단어를 사용하고 싶습니다. 예를 들어, 내 텍스트 파일은 다음과 같다 경우 :텍스트 파일에서 읽기 및 배열에 데이터 삽입

Gender: M 
First Name: Gordon 
Last Name: Freeman 
Job: Engineer 
Gender: F 
First Name: Sally 
Last Name: Reynolds 
Job: Scientist 

이 목록은 계속적으로 갈 수 :

M 
Gordon 
Freeman 
Engineer 
F 
Sally 
Reynolds 
Scientist 

난과 같이 배열 및 출력 그것으로 각 줄을 추가 할 수 있도록하려면 , 그러나 2 개는 지금 좋다. ,

struct PeopleInfo 
{ 
    char gender; 
    char name_first [ CHAR_ARRAY_SIZE ]; 
    char name_last [ CHAR_ARRAY_SIZE ]; 
    char job [ CHAR_ARRAY_SIZE ]; 
}; 

내가 (성시 각 부분에서 중지하는 프로그램을 알려줄 수있는 구분 기호 또는 무언가를 사용할 필요가 있는지 확실하지 않습니다 :

나는 현재 정보를 보유하는 구조체를 사용하고 있습니다 이름, 성 등). ifline에 getline 함수를 사용할 수 있습니까? 내 자신의 코드에서 구현하는 데 문제가 있습니다. 나는 어디에서 시작해야할지 모르겠다. 나는 지금 이런 식으로 사용하지 않아도된다. Frantically 유사한 문제를 찾기 위해 교과서와 Google을 통해 검색하지만, 지금까지 나는별로 행운이 없었어요. 내가 발견 한 질문과 코드로 내 게시물을 업데이트 할 것입니다. 얻을

답변

3

내가 user1200129 @ 생각은 올바른 궤도에 있지만 확실히 모든 조각은 아직 조립 못했습니다 .

난 그냥 조금 구조를 변경 것

:

struct PeopleInfo 
{ 
    char gender; 
    std::string name_first; 
    std::string name_last; 
    std::string job; 
}; 

그럼 내가 그 구조 operator>>에 과부하를 줄 :

std::istream &operator>>(std::istream &is, PeopleInfo &p) { 
    is >> p.gender; 
    std::getline(is, p.name_first); 
    std::getline(is, p.name_last); 
    std::getline(is, p.job); 
    return is; 
} 

당신이 그들을 표시 할 수 있기를 원하기 때문에, I

std::ostream &operator<<(std::ostream &os, PeopleInfo const &p) { 
    return os << "Gender: " << p.gender << "\n" 
       << "First Name: " << p.name_first << "\n" 
       << "Last Name: " << p.name_last << "\n" 
       << "Job: " << p.job; 
} 

그런 다음 AF 읽기 : '너무 그렇게하는 operator<<를 추가 거라고

std::ifstream input("my file name"); 

std::vector<PeopleInfo> people; 

std::vector<PeopleInfo> p((std::istream_iterator<PeopleInfo>(input)), 
          std::istream_iterator<PeopleInfo(), 
          std::back_inserter(people)); 

은 마찬가지로, 벡터에서 사람들의 정보를 표시하는 것은 같은 간다 : 데이터의 전체 ILE 이런 식으로 뭔가 할 수 있습니다

std::copy(people.begin(), people.end(), 
      std::ostream_iterator<PeopleInfo>(std::cout, "\n")); 
+0

당신이''''에''p. – molbdnilo

+0

@molbdnilo : 예, 감사합니다. 결정된. –

0

그럼 당신은 시작 :

// Include proper headers here 
int main() 
{ 
    std::ifstream file("nameoftextfilehere.txt"); 
    std::string line; 
    std::vector<std::string> v; // Instead of plain array use a vector 

    while (std::getline(file, line)) 
    { 
     // Process each line here and add to vector 
    } 

    // Print out vector here 
} 
+0

흠을, 모든 권리는 감사합니다. 나는 이것을 가지고 놀 것입니다. – Hydlide

1

구조체는 정보를 저장하기위한 배열보다 더 할 수있다.

struct person 
{ 
    std::string gender; 
    std::string first_name; 
    std::string last_name; 
    std::string position; 
}; 

그런 다음 사람의 벡터를 가지고 반복 할 수 있습니다.

+0

죄송합니다. 내가 구조체를 사용하고 있습니다.정보를 추가했습니다. 벡터에 대해서도 살펴볼 것입니다. – Hydlide

0

bool maleFlag 및 bool femaleFlag와 같은 플래그를 사용하고 행에 'M'또는 'F'만 읽으면 true 및 false로 설정할 수도 있습니다. 그러면 어떤 성별을 연결할 지 알 수 있습니다 따르는 이름.

0

또한 다른 스트림로 표준 : : ifstream 파일을 사용할 수 있습니다

//your headers 
int main(int argc, char** argv) 
{ 
    std::ifstream file("name.txt"); 
    std::string line; 
    std::vector<std::string> v; // You may use array as well 

    while (file.eof() == false) { 
     file >> line; 
     v.push_back(line); 
    } 

    //Rest of your code 
    return 0; 
} 
+0

'while (file >> line)'이 더 좋을 것입니다. – devil