Datei I / O in C ++
Frage
Alles, was ich tun möchte, ist der Inhalt von readme.txt 20 mal gedruckt .. bitte helfen.
int main()
{
ifstream myfile;
string line;
int i;
myfile.open ("readme.txt");
if (myfile.is_open()){
while (i<20){
i++;
if(!myfile.eof()){
cout << "asdasd" << "\t";
myfile.seekg(0, ios::beg);
}
getline (myfile,line);
cout << line << endl;
}
cout << endl;
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Lösung
Dies macht die Arbeitsplätze Mann:
#include <iostream>
#include <vector>
#include <fstream>
int main()
{
std::ifstream myfile;
std::string content;
std::string line;
myfile.open ("Readme.txt");
if (myfile.is_open()){
if(!myfile.eof())
{
getline (myfile,line);
content.append(line);
}
while (!myfile.eof()){
getline (myfile,line);
content.append("\n");
content.append(line);
}
myfile.close();
for(int i = 0; i < 20; i++)
std::cout << content << std::endl;
}
else std::cout << "Unable to open file";
return 0;
}
Andere Tipps
Es gibt mehrere Probleme mit Ihrem Code. erster i nicht initialisiert. Zweite Lesung den Inhalt der Datei getan werden sollte, einmal vor der Schleife nicht nach, sollten Sie den Inhalt der Datei drucken, wo asdasd gedruckt wird, um den Inhalt der Datei so oft wie die Schleifendurchläufe gedruckt zu sehen.
Sie sollten diesen (Pseudo-Code) tun:
if(file is open)
{
for(int i = 0; i<20; ++i)
{
while(getline(file, line))
{
print line
}
seek to 0
}
close file
}
Edit: In der Tat Ihr eigentliches Problem ist die nicht initialisierte Variable i
. Der tiefere Grund war, dass man while
eingesetzt, wo for
geeignetere war
Nach dem Code, den Sie drucken asdasd
, wenn Sie nicht am Ende der Datei sind.
Sie haben
int i;
und diese i
nicht initialisiert wird.
if(!myfile.eof())
Sie möchten vielleicht die !
verlieren. Sie sind auf jeder Schleife an den Anfang der Datei zurückgespult wird.
(Kiril hat auch einen Punkt hier ...)
#include <iostream>
#include <algorithm>
#include <fstream>
#include <iterator>
#include <vector>
int main ()
{
std::ifstream ifs("readme.txt");
std::vector<char> filebuffer;
ifs.seekg (0, std::ios::end);
size_t size = static_cast<size_t>(ifs.tellg());
ifs.seekg (0, std::ios::beg);
filebuffer.resize(size);
ifs.read(&filebuffer[0], size);
for(int i = 0; i < 20; ++i)
std::copy(filebuffer.begin(), filebuffer.end(),
std::ostream_iterator<char>(std::cout));
return 0;
}
Nicht sicher, ob dies das Problem zu beheben, aber Ihre Struktur ist ziemlich schlecht. Verwenden Sie eine Weile, wenn Sie nicht wissen, wie oft Sie eine Schleife wünschen, andernfalls eine Verwendung für Schleife. So etwas wie unten sollte ok sein
int main()
{
ifstream myfile;
string content;
string line;
myfile.open ("readme.txt");
while(!myfile.eof()){
getline (myfile,line);
content += line;
}
myfile.close();
for(int i = 0; i < 20; i++)
{
cout << content << endl;
}
return 0;
}
Hope, das hilft.
Ich weiß nicht, warum Sie wollen das tun, aber dieser Code funktioniert:
#include <iostream>
#include <fstream>
using std::cout;
using std::endl;
int main(int argc,char* argv[])
{
std::fstream myfile;
for (int i=0; i<20; i++)
{
myfile.open("main.cpp",std::fstream::in);
if (myfile)
{
cout << myfile.rdbuf() << endl;
cout << "FINISH" << endl;
}
else
cout << "Error" << endl;
myfile.close();
}
return 0;
}
Wenn die Datei nicht während der Iteration zu ändern, das ist noch besser
#include <iostream>
#include <fstream>
using std::cout;
using std::endl;
int main(int argc,char* argv[])
{
std::fstream myfile;
myfile.open("main.cpp",std::fstream::in);
for (int i=0; i<20; i++)
{
if (myfile)
{
cout << myfile.rdbuf() << endl;
cout << "FINISH" << endl;
}
else
cout << "Error" << endl;
}
myfile.close();
return 0;
}