2016-10-21 3 views
-2

이 프로그램에서 텍스트 파일을 추가하려고 시도하고 있습니다. 모든 반복마다이 파일을 새 줄로 만듭니다. 매번 추가 될 것이지만 새로운 행마다 새로운 행을 추가하지는 않습니다.C++로 파일 읽기/쓰기.

어쨌든 텍스트 파일의 모든 줄을 읽으려고합니다.하지만 첫 단어 만 읽지는 않습니다. 당신이 getline을 사용할 수 있습니다

void Write_Score(string name, int score) 
{ 
ofstream outfile; 
outfile.open("Highscores.txt", ios::app); 
outfile << name << "\t" << score << std::endl; 
outfile.close(); 
} 

은 전체 라인을 읽으려면 :

#include<iostream> 
#include<string> 
#include<fstream> 
#include<ios> 
using namespace std; 

/* 
Return types: void 
Types: 1 string, 1 int 
Purpose: write to the highscores.txt 
*/ 
void Write_Score(string name, int score); 

/* 
Return type: Void 
Types: N/A 
Purpose: Read the highscores.txt 
*/ 
void Read_Score(); 

int main() 
{ 
string usr_name; 
int usr_score = 0; 
cout << "Enter your name: "; 
cin >> usr_name; 
cout << endl; 
cout << "Enter your score: "; 
cin >> usr_score; 
cout << endl; 
Write_Score(usr_name, usr_score); 
cout << endl << endl << endl; 
Read_Score(); 

} 

void Write_Score(string name, int score) 
{ 
ofstream outfile; 
outfile.open("Highscores.txt", ios::app); 
outfile << name << "\t" << score; 
outfile.close(); 
} 

void Read_Score() 
{ 
string name; 
int score = 0; 
ifstream infile; 
infile.open("Highscores.txt", ios::in); 
infile >> name >> score; 
cout << name << "\t" << score << endl; 
infile.close(); 
} 
+0

'outfile << name << "\ t"<< score << endl; 및 읽기를위한 getline()입니다. –

+0

저주. 'outfile << name << "\ t"<< score << '\ n "';'Crom이 플러시를 분류하자. – user4581301

+0

좀 더 간결한 코드 샘플을 제공하거나 향상시키고 자하는 코드의 특정 부분을 말하고/주석하십시오. –

답변

관련 문제