Program not properly detecting newline characters

I have the following text file:

1
2
3
4
5
6
7
8
9
10
11
12
7
9
46123
85321
3872663
69826
2222222222222222222222222
7777777777766666666666666
9999999999999999999999999
9999999999999999999999999
5555555555555555555555555
999999999999999


This is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

const int NUMBERS = 26;

ifstream infile;
ofstream outfile;

void read(int first_array[], int second_array[], int sum_array[])
{
    int i = 0, k = 0, convert = 0;
    char check;

    infile.open("MyNumbers.txt");

    if (!infile)
    {
        cout << "Failed to open data file!\n";
        cin.get();
        exit(0);
    }

    while (infile.get(check))
    {
        if (check != '\n')
        {
            convert = check - '0';
            first_array[i] = convert;

            i++;
        }

        else
        {
            break;
        }

    }

    for (int j = 0; j < NUMBERS; j++)
    {
        cout << "First array: " << first_array[j] << endl;
    }

    infile.close();
}

int main()
{
    int first_line[NUMBERS] = {0}, second_line[NUMBERS] = {0}, sum_line[NUMBERS] = {0};

    read(first_line, second_line, sum_line);

 //   cin.get();
    return 0;
}


The code is suppose to read in the first number (number 7) into the program and store it in the first_array (after converting it to an integer 7 by subtracting the character '0'). Then, the next character collected from infile.get() should be the newline \n character. But, when I run it in my terminal, it has a -35 in the second position. I don't know why it is getting a -35 in the second position because it is suppose to skip to break if a newline is encountered. If I use the integer 13, it seems to work out properly, but I really want to use the \n newline character. Can anyone help me with this?
I assume you are using Windows, but I don't know which compiler.

In a text file on Windows, line breaks are usually represented by a pair of characters, the CR-LF (carriage-return, line-feed). These have the decimal values of 13 and 10, or in C++, '\r' and '\n'.

When a file is opened in text mode, you can normally consider just the '\n' as there is some automatic transformation going on. But if the file is opened in binary mode, you will get to see exactly what is in the file, with no translation.

Now I tested your code, and the only way I can reproduce your result is by changing line 16 to
infile.open("input.txt", ios::binary); because the default is to open in text mode. It seems somehow you have the file open in binary mode.

A suggested workaround is to change line 27 to
if (check != '\n' && check != '\r')

hmm... it seems to me that your numbers are too great for a "int" (on gcc, if I remember well, an int is 2bytes long, so - seeing it is "signed" - it should be able to manage values up to a value around 35,000)... the memory limit for "int" however depends from compiler to compiler (while "short", "long" and "char" usually have the same size for all compilers.... if I remember well: short = 2bytes; long = 4bytes)
Topic archived. No new replies allowed.