C++ ifstream; every '\n' character is printed twice

Kirill

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!

Roman

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

reading a C++ ifstream twice?

From Dev

C++ Output being printed twice

From Dev

C++ ifstream read 2 character numbers

From Dev

How to insert a character every N characters in a string in C++

From Dev

Insert a character every N characters in a string in C++ changing N character

From Dev

Insert a character every N characters in a string in C++ changing N character

From Dev

Why do strings get printed twice when using printf in C?

From Dev

Why is the line printed twice?

From Dev

Message in macro printed twice

From Dev

C - Remove a newline character that has already been printed

From Dev

How to change a character in C++ console that was printed to the screen earlier

From Dev

cryptic hexadecimal character printed to file by c++ ofstream

From Dev

invalid character are printed in program

From Dev

Why is the line 12 is printed twice?

From Dev

Data printed twice in first iteration

From Dev

Repeat every character in string for N times

From Dev

why using a ifstream object twice causes error?

From Dev

Repeat content in every printed sheet

From Dev

Repeat content in every printed sheet

From Dev

Convert a printed message into a character vector

From Dev

Convert a printed message into a character vector

From Dev

Why is this statement printed twice in while loop?

From Dev

reading input with Scanner; output is printed twice

From Dev

Why is inherited value printed out twice?

From Dev

C++: ifstream as a parameter

From Dev

What is the best algorithm for removing every kth character in a string of length N?

From Dev

Split one column by every into "n" columns with one character each

From Dev

Space and new line after every n'th character

From Dev

Put a certain character after every n words in Oracle

Related Related

  1. 1

    reading a C++ ifstream twice?

  2. 2

    C++ Output being printed twice

  3. 3

    C++ ifstream read 2 character numbers

  4. 4

    How to insert a character every N characters in a string in C++

  5. 5

    Insert a character every N characters in a string in C++ changing N character

  6. 6

    Insert a character every N characters in a string in C++ changing N character

  7. 7

    Why do strings get printed twice when using printf in C?

  8. 8

    Why is the line printed twice?

  9. 9

    Message in macro printed twice

  10. 10

    C - Remove a newline character that has already been printed

  11. 11

    How to change a character in C++ console that was printed to the screen earlier

  12. 12

    cryptic hexadecimal character printed to file by c++ ofstream

  13. 13

    invalid character are printed in program

  14. 14

    Why is the line 12 is printed twice?

  15. 15

    Data printed twice in first iteration

  16. 16

    Repeat every character in string for N times

  17. 17

    why using a ifstream object twice causes error?

  18. 18

    Repeat content in every printed sheet

  19. 19

    Repeat content in every printed sheet

  20. 20

    Convert a printed message into a character vector

  21. 21

    Convert a printed message into a character vector

  22. 22

    Why is this statement printed twice in while loop?

  23. 23

    reading input with Scanner; output is printed twice

  24. 24

    Why is inherited value printed out twice?

  25. 25

    C++: ifstream as a parameter

  26. 26

    What is the best algorithm for removing every kth character in a string of length N?

  27. 27

    Split one column by every into "n" columns with one character each

  28. 28

    Space and new line after every n'th character

  29. 29

    Put a certain character after every n words in Oracle

HotTag

Archive