0

When reading a file through getline, \n is written to the resulting character. The data in the file is written like this (in this example, _ represent spaces):

2_
c_3_
a_1_

The first digit in the file is the number of records.

Code:

getline(autorisationFile, temp, ' ');

while (getline(autorisationFile, temp, ' ')) {
        arr[i].login = temp;
        getline(autorisationFile, temp, ' ');
        arr[i].password = temp;
        i++;
    }
Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
  • 2
    Did you read the documentation of [`std::getline`](https://en.cppreference.com/w/cpp/string/basic_string/getline)? It extracts until "_the next available input character is `delim`, as tested by `Traits::eq(c, delim)`, in which case the delimiter character is extracted from input, but is not appended to `str`._" In your case `delim` is `' '`, and not `'\n'`, so `'\n'` is extracted, and appended to `str`. Relevant read (possibly duplicate): https://stackoverflow.com/questions/37957080/can-i-use-2-or-more-delimiters-in-c-function-getline – Algirdas Preidžius Dec 10 '20 at 15:13
  • Maybe, consider nested loops where the outer loop reads a line and uses a `std::istringstream` for the inner loop. Btw. `std::getline()` with `' '` as delim... The formatted input operators (`>>`) use whitespace as delimiter by default (and, btw.² whitespace covers space as well as `'\n'`). So, `std::getline()` might be over-engineered in this case. – Scheff's Cat Dec 10 '20 at 15:40
  • Could you state your question more explicitely please? I'm guessing you want to know how to have the field `password` filled without the `\n` in it? – Jean-Benoit Harvey Dec 10 '20 at 16:14

1 Answers1

0

You might want to change the way you parse the file a bit...
If you process it line by line with getline first, and then have << split the line where there are whitespace.

while (std::getline(authorisationFile, temp)){
        std::istringstream line(temp);
        line >> arr[i].login;
        line >> arr[i].password;
        i++;
    }

Playground with proof of work (edited a bit the initial code to work around the missing parts ;) )