Ich versuche, dieses Programm eine Textdatei anhängen, so dass es eine neue Zeile mit jeder Iteration. Es wird jedes Mal angefügt, aber nicht für jede neue Zeile neue Zeilen hinzugefügt.Lesen/Schreiben einer Datei in C++
Ich versuche, es jede Zeile in der Textdatei auszulesen, obwohl es nichts als das erste Wort lesen und das anzeigen wird.
Das ist, was ich bisher getan habe:
#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();
}
'outfile << name <<" \ t "<< score << endl;' und 'getline()' zum Lesen. –
Flüche. Ich wollte 'outfile << name << "\ t" << score <<' \ n'' auflegen und Crom die Spülung aussortieren. – user4581301
Geben Sie bitte ein präziseres Codebeispiel an oder teilen Sie die spezifischen Teile Ihres Codes mit, die Sie verbessern möchten. –