0

I'm writing a program that reads a text file and outputs the data. For example, here's what one "entry" of the data looks like:

Alexander, Maurice
DB
1
0
0
0
0

I've written some code to read this first entry and output it.

struct playerType{
  string name;
  string position;
  int touchdowns;
  int catches;
  int yardsPass;
  int yardsRsh;
  int yardsRcv;
};

int main(){
  ifstream inData;
  inData.open("data.txt");
  playerType players[10];

  getline(inData >> ws, players[0].name, '\n');
  inData.clear();
  cout << players[0].name << endl;
  inData >> players[0].position;
  cout << players[0].position << endl;
  inData >> players[0].touchdowns;
  cout << players[0].touchdowns << endl;
  inData >> players[0].catches;
  cout << players[0].catches << endl;
  inData >> players[0].yardsPass;
  cout << players[0].yardsPass << endl;
  inData >> players[0].yardsRsh;
  cout << players[0].yardsRsh << endl;
  inData >> players[0].yardsRcv;
  cout << players[0].yardsRcv << endl;

  cout << endl << "After:" << endl;
  cout << players[0].name << players[0].position << players[0].touchdowns
  << players[0].catches << players[0].yardsPass << players[0].yardsRsh
  << players[0].yardsRcv << endl;
}

I'm not sure why, but when I output each part of the struct on separate lines, the proper values are outputted. However, when I output everything on a single line, the name gets overlapped by subsequent variables: enter image description here

I'm sure I'm just making a beginner's mistake somewhere, but I can't seem to figure out why the output is like this. I was under the impression the variables would output one after another, instead of overlapping each other.

Samuel L.
  • 123
  • 1
  • 7
  • 1
    Looks like there is a CR character (`\r`) at the end of the name. – 1201ProgramAlarm Jun 08 '21 at 15:39
  • As @1201ProgramAlarm stated, there must be a `\r` char. Could you please run `hexdump -C data.txt` and give us the output of that command? – Nikita Demodov Jun 08 '21 at 15:45
  • @NikitaDemodov Here's the output of that command: https://imgur.com/xamNe3m – Samuel L. Jun 08 '21 at 15:57
  • I've heard of the ```\r``` character before, but have never worked with it before. What caused the ```\r``` character to be saved into the variable? How can I prevent this from happening? – Samuel L. Jun 08 '21 at 15:58
  • @SamuelL. See that `0d`(second line, third byte)? That's a barrage return(`\r`) char. It sets the position of the cursor to the beginning of the line, but in combination with `\n` is also used by Windows for new-lines(Win: `\r\n`, UNIX: `\n`). It seems that your OS is not able to detect this combination of chars and therefor interprets the `\r` as what it actually stands for. That's why I on my Mac can see it, but you on your OS can't. TL;DR: blame Microsoft and your OS for being too "stupid". – Nikita Demodov Jun 08 '21 at 16:08
  • @SamuelL. I summed it up in an answer. – Nikita Demodov Jun 08 '21 at 16:17
  • @NikitaDemodov It is a bit unfair to blame Microsoft for that. The `\r\n` sequence goes back to the initial IBM-PC, where the two characters were used to do two distinct actions with the print head on the printer. This behavior was likely copied from earlier hardware platforms, like the mainframe systems in common use at the time. – 1201ProgramAlarm Jun 08 '21 at 18:43

1 Answers1

1

You'll have to replace all the carriage-return + new-line character combinations(which are used as new-lines on Windows but aren't understood by (some distros of) Linux) with simple new-lines. Here's some code for doing that:

#include <string>
#include <regex>

name = std::regex_replace(name, std::regex("\r\n"), "\n");
Nikita Demodov
  • 553
  • 5
  • 17
  • Note: You'd also have to do this for position – Nikita Demodov Jun 08 '21 at 16:17
  • I see, let me give this a try in my code. Could I ask why I don't need to do this for the integer variables? – Samuel L. Jun 08 '21 at 17:37
  • 1
    @SamuelL. because the `\r` is part of the string -- like a letter. Instead of being `"Alexander, Maurice"`, your string is `"Alexander, Maurice\r"`. The integer is just a number -- it doesn't contain letters. – Nikita Demodov Jun 09 '21 at 13:04