Блог пользователя Robin

Автор Robin, история, 4 года назад, По-английски

So I was debugging this code and managed input/outputs in separate files. I ran the same code with same flags in codeblocks and WSL ubuntu 18.04, they gave me two different outputs.

My Code

#include<bits/stdc++.h>

using namespace std;

int main() {
#ifdef ROBIN
    freopen("in", "r", stdin);
    freopen("out", "w", stdout);
#endif // ROBIN
    int t, d = 0;
    cin >> t;
    getchar();
    while (t--) {
        string ss, tt;
        getline(cin, ss);
        getline(cin, tt);
        cout << "SS: " << ss << endl;
        cout << "TT: " << tt << endl;
    }
}

Input

3
Tom Marvolo Riddle
I am Lord Voldemort
I am not Harry Potter
Hi Pretty Roar to man
Harry and Voldemort
Tom and Jerry and Harry

Output (Code::Blocks)

SS: Tom Marvolo Riddle
TT: I am Lord Voldemort
SS: I am not Harry Potter
TT: Hi Pretty Roar to man
SS: Harry and Voldemort
TT: Tom and Jerry and Harry

Output (WSL)

SS: 
TT: Tom Marvolo Riddle
SS: I am Lord Voldemort
TT: I am not Harry Potter
SS: Hi Pretty Roar to man
TT: Harry and Voldemort



I would appreciate it if someone gives me a proper explanation of this. Thanks.
Flags:
-DROBIN
-std=c++14
-O2

  • Проголосовать: нравится
  • +14
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by Robin (previous revision, new revision, compare).

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by Robin (previous revision, new revision, compare).

»
4 года назад, # |
Rev. 6   Проголосовать: нравится +16 Проголосовать: не нравится

Nevermind, got the answer. Posting here if anyone needs to know:

Newlines in windows need 2 bytes, while those in linux generally need 1 byte. Before cin >> t I'm using getchar(), which reads only 1 character -> 1 byte. So my getline() reads the other character and I get an extra line because of that. I can convert the input file (from CR-LF) to use UNIX style file endings (LF), or use an extra getchar(), after cin >> t.
Reference: https://superuser.com/questions/1091980/why-are-windows-line-breaks-larger-than-unix-line-breaks

Thanks to https://web.facebook.com/oneshadab for the idea.

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +17 Проголосовать: не нравится

    Yep. That's why you want to use getline() instead of getchar() whenever you're reading files split into lines — it handles the endline characters correctly automatically.