2016-10-24 2 views
-1

텍스트 파일을 프로그램으로 읽어들이는 과정을 이해하는 데 어려움을 겪고 있습니다. 아래쪽 get_answer_key_array, 텍스트 파일의 맨 위 줄을 읽고 배열에 넣으려고합니다. bacb여러 유형의 데이터를 배열로 읽는 중

첫 번째 줄의 응답 키가됩니다이 프로그램에서 테스트 모든 텍스트 파일 abcbd

ABDBCBDBABABCBABCB

밥 바비 adcad :

텍스트 파일은 다음과 같이 보인다. 첫 줄 뒤에 오는 모든 줄에는 이름, 공간, 성, 공백, 성적 및 누락 된 성적이 도착할 때 "-"로 바뀝니다.

현재 첫 번째 줄을 가져 와서 대답 키 배열에 넣고 있습니다. 첫 번째 줄을 끝내고 나면 사람들의 이름, 성 및 대답을 세 개의 병렬 배열에 넣을 수 있음을 알고 있습니다. 첫 번째 새 줄을 확인하는 올바른 방법을 찾기 위해 문제가 생겨서 대답 키를 얻을 수 있습니다.

이제는 필요한 모든 배열을 얻기 위해 get_answer_key_array를 변경했습니다. 현재 answer_key []의 첫 번째 배열에 맨 위 줄 (답안 키)을 가져 오려고합니다. 필자는 getline 함수를 구현하려고했지만 최상위 라인 만 얻는 방법을 알아 내려고했습니다. 첫 번째 줄의 데이터를 배열로 전송하려면 첫 번째 endline에서 eof() 루프를 계속 유지할 수 있습니까? 또한 내 Answer_key [i] = key; 내가 필요로하는 다른 것을 바꿀 필요가있다!

배열로 맨 위의 줄을 얻는 방법을 알아 낸 후에는이 프로세스를 사용하여 나머지 워크 플로 (이름 및 답변)를 다음 워크 플로를 통해 별도의 배열로 가져와야합니다.

void get_input_file(ifstream &in_stream); //gets the text file name from the user 
void get_arrays(ifstream &in_stream, int answer_key[], string first_name[], string last_name[], int answers[], int &count); //brings the data from text file into all of the parallel arrays 
//void score_grader(int &target, string first_name[], string last_name[], int answers[], int &count, int &score); 
//void letter_grade(int &score, int &letter_grade); 
//void student_report(int &target, string first_name[], string last_name[], int answers []); 

int main() 
{ 
    ifstream in_stream; 
    int answer_key[30], count = 0, score = 0; //initializing the answer key array up to 30 answers 
    string first_name[20];  //initializing the first name array 
    string last_name[20];  //initializing the last name array 
    int answers[30];   //initializing the answers array 

    cout << "Welcome to the Test Grader." << endl; //welcome message 
    get_input_file(in_stream); //function call to get the file name 
    get_arrays(in_stream, answer_key, first_name, last_name, answers, count); //function call to create the arrays 
} 


void get_input_file(ifstream &in_stream) { 
    string file_name; //initializing the file name string 

    do { 
     cout << "Enter the file name you would like to import the data from: " << endl; //asks user to input name of file 
     cin >> file_name;   //user types name of file 
     in_stream.open(file_name.c_str());  //program opens the stream 

     if(in_stream.fail()) { 
      cout << "Error finding file, try again.\n"; //if failed, asks user for file name again 
      continue; //continues back to the do loop 
     } 
     break; 
    } while(true); 

    cout << "File Obtained: " << file_name << endl; //alerts user of file success with printed name 


} 

void get_arrays(ifstream &in_stream, int answer_key[], string first_name[], string last_name[], int answers[], 
int &count) { 
    int i = 0; 
    string key; //This will be the storage variable for the first line of text file 
    if (in_stream.is_open()) {  //if the stream is open 

     getline(in_stream, key); 

     cout << "Testing: " << key << endl; 

     while(!in_stream.eof()) { 
      i++; 
      in_stream >> first_name[i] >> last_name[i] >> answers[i]; 
     } 
    } 
    cout << first_name[1] << " " << last_name[1] << " " << answers[1] << endl; 
    in_stream.close(); 
} 
+1

입니다. – NathanOliver

+0

'in_stream >> answer_key [i];'는 파일에서 숫자를 읽으려고합니다. 그게 정말 의도적 인가요? –

+0

'getline'은 전체 줄을 잡는 데 사용됩니다. 그런 다음, 반복문을 사용하여 답변 키의 모든 문자를 꺼낼 수 있습니다. – AndyG

답변

0

아래 프로그램의

in_stream >> first_name[i] >> last_name[i] >> answers[i]; 
while(!in_stream.eof()) { 
    i++; 
    in_stream >> first_name[i] >> last_name[i] >> answers[i]; 
} 
in_stream.close(); 

START 당신은 단순히 첫 번째 줄이 방법

void get_answer_key_array(ifstream &in_stream, char *answer_key, int &count) { 

    in_stream >> answer_key; 
    count = strlen(answer_key); 
} 
를 읽을 수 있습니다

answer_key 배열의 형식은 char이어야합니다.

당신이`getline`를 사용해야 데이터의 줄을 읽을 필요가있는 경우 그리고 endline 문자는 '\ n'을하지 '/ n'을

+0

_ "answer_key 배열은 char 유형이어야합니다."_ 나는'std :: string'을 제안하려고합니다. –

+0

코멘트를 보내기 전에이 부분의 구조를 변경했습니다. – Logan

관련 문제