Theta Onyx
Theta Onyx

Reputation: 3

C++ cout char 'return' character from file appears twice

I'm trying to create a program that encrypts files based on how Nazi Germany's Enigma machine worked, but without the flaw :P.

I have a function that gets a character at n point in a file, but when it returns a return character and I cout << it, it's like it hit enter twice. IE if I loop cout-ing from i++ points in a file the individual lines in the terminal appear separated

by more returns

than one.

Here's the function:

char charN(string pathOf, int pointIn){
    char r = NULL;
      // NULL so I can tell when it doesn't return a character.
    int sizeOf; //to store the found size of the file.
    ifstream cf; //to store the Character Found.
    ifstream siz; //used later to get the size of the file

    siz.open(pathOf.c_str());

    siz.seekg(0, std::ios::end);
    sizeOf = siz.tellg();  // these get the length of the file and put it in sizeOf.

    cf.open(pathOf.c_str());
    if(cf.is_open() && pointIn < sizeOf){ //if not open, or if the character to get is farther out than the size of the file, let the function return the error condition: 'NULL'.
        cf.seekg(pointIn); // move to the point in the file where the character should be, get it, and get out.
         cf.get(r);
         cf.close();
    }
    return r;
}

It works correctly if I use cout << '\n', but what's different about returns from a file and '\n'? Or is there something else I'm missing? I've been googling about but I can't find anything remotely similar to my problem, thanks in advance.

I'm using Code::Blocks 13.12 as my compiler if that matters.

Upvotes: 0

Views: 122

Answers (1)

Kevin Aud
Kevin Aud

Reputation: 398

Is this is on a windows machine? In windows new lines in text files are representing by \r\n.

  • \r = carriage return
  • \n = line feed

It's possible that you are couting each one separately and that the output buffer is creating a new line for each one.

Upvotes: 1

Related Questions