Ich bin hier auf this question gestoßen und hatte eine weitere Frage über die Antwort geben (Ich kann nicht kommentieren, da ich Stackoverflow neu bin). Der Typ, der darauf geantwortet hat, scheint recht zu haben, < < >> für cin
und cout
zu ersetzen. Aber das Problem, das ich habe, ist, dass alle Semikolons nicht in der neuen Ausgabedatei angezeigt werden.I/O Stream String Manipulation (Korrekte Cin/Cout Operatoren <</>>)
Ich weiß, dass während std::getline(input, command, ';')
alle Semikolons löscht, aber theyre sollte am Ende mit der else-Anweisung zu setzen, aber es nicht ersetzt wird, wenn ich es laufen. Und wenn ich ';' aus der getline
Anweisung alles in der Ausgabedatei vermasselt.
Wie zeigen Sie, dass das Semikolon zeigt?
void print(ifstream& input,ofstream& output)
{
bool first = true;
std::string command;
while(std::getline(input, command, ';'))
{ // loop until no more input to read or input fails to be read
if (command.find("cin")!= std::string::npos)
{ // found cin somewhere in command. This is too crude to work. See below
size_t pos = command.find("<<"); // look for the first <<
while (pos != std::string::npos)
{ // keep replacing and looking until end of string
command.replace(pos, 2, ">>"); // replace with >>
pos = command.find("<<", pos); // look for another
}
}
else if (command.find("cout")!= std::string::npos)
{ // same as above, but other way around
size_t pos = command.find(">>");
while (pos != std::string::npos)
{
command.replace(pos, 2, "<<");
pos = command.find(">>", pos);
}
}
if (! first)
{
output << command; // write string to output
}
else
{
first = false;
output << ';' << command; // write string to output
}
}
}
Die Funktion 'std :: getline' ändert den Inhalt der Datei nicht. –
Bitte bearbeiten Sie Ihre Frage mit dem Code, mit dem Sie Probleme haben. –
Entschuldigung. Bearbeitet. – Deegeeek