i've created a little program that implements operator[] to read files by indexing. There's code:
#include <iostream>
#include <fstream>
using namespace std;
class Findexer
{
ifstream* f;
public:
Findexer(string s)
{
f = new ifstream(s, ios_base::in);
if (!f->is_open())
cout << "Can't open file: " << s;
}
~Findexer()
{
delete f;
}
char operator[](size_t ind)
{
f->seekg(ind);
return f->get();
}
};
int main()
{
Findexer f("file.cpp"); //file with this program; it is the same with any other file
for (int i = 0; f[i] != EOF; ++i)
cout << f[i];
}
It works fine except that it prints every new line character twice. There's beginning of output:
#include <iostream>
#include <fstream>
using namespace std;
class Findexer
I've no idea why it happens, could not google anything useful. :( Please help!
I guess you are running it in windows and you use MSVS?
Most likely it's a problem with DOS/UNIX encoding of newlines. Check in the editor that shows control characters how exactly newlines are encoded in file.cpp. On linux system your code runs fine and prints the file correctly.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments