file i/o in c++
Question
all i want to do is print the contents of readme.txt 20 times.. please help.
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;
}
Solution
This does the jobs man :
#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;
}
OTHER TIPS
there are several problems with your code. first i isn't initialized. Second reading the contents of the file should be done once before the loop not after, you should print the contents of the file where asdasd is printed to see the contents of the file printed as many times as the loop runs.
You should do this (pseudocode):
if(file is open)
{
for(int i = 0; i<20; ++i)
{
while(getline(file, line))
{
print line
}
seek to 0
}
close file
}
Edit: In fact your real problem is the uninitialized variable i
. The deeper reason was that you used while
where for
was more appropriate
According to your code, you are printing asdasd
if you are NOT at the end of the file.
You have
int i;
and this i
is not initialized.
if(!myfile.eof())
You might want to lose the !
. You are rewinding to the beginning of the file on each loop.
(Kiril also has a point here...)
#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;
}
Not sure if this will fix your problem but your structure is pretty bad. Use a while when you don't know how many times you want to loop, otherwise use a for loop. Something like below should be ok
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 this helps.
I don't know why you wanna do that but this code works:
#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;
}
If the file is not changing during the iteration, this is even better
#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;
}