0

I have a file which contain these data :

6
1231234213 12
2312354234 23
3254132515 43

I want to store all these data into an array :

int main() {
    ifstream infile("data.txt");
    ofstream outfile("final.txt");
    int sn = 0;
        infile >> sn;
    int* arr;
    arr = new int[sn];
    for (int i = 0; i < sn; i++)
    {
        infile >> arr[i];
    }
    for (int i = 0; i < sn; i++)
    {
        cout << arr[i] << endl;
    }
}

but I'm getting this instead of those data :

1231234213
12
2147483647
-842150451
-842150451
-842150451 
6
  • Always test the stream state after an IO transaction. Error conditions that you spend time don't trapping and handling generally waste orders of magnitude more time debugging. Commented Jan 6, 2023 at 19:28
  • 4
    note that 2312354234 is bigger than integer max value 2147483647 (0x7FFFFFFF) Commented Jan 6, 2023 at 19:35
  • 2
    Side note: -842150451 is a magic debug value. In hex it is CDCDCDCD, a pattern used to help you trap uninitialized heap-allocated values. When you see a nice repetition like this or a huge weird number, convert it to hex and see if it is more recognizable. Here's a list of well-known debug values. Commented Jan 6, 2023 at 19:39
  • The output of 2147483647 is also a helpful number. It is 7FFFFFFF, signifying exactly what Gian is warning you of. Commented Jan 6, 2023 at 19:41
  • 1
    You could switch to use int64_t instead of int if your data requires 64 bit integers. Commented Jan 6, 2023 at 19:42

1 Answer 1

2

When testing with an array of long long int your problem disappears:

int main() {
    std::ifstream infile("data.txt");
    int n = 0;
    
    infile >> n;

    long long int *arr = new long long int[n];

    for (int i = 0; i < n; i++) {
        infile >> arr[i];
    }

    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << std::endl;
    }

    return 0;
}

Output:

1231234213
12
2312354234
23
3254132515
43

You might make things easier on yourself by using a std::vector.

int main() {
    std::ifstream infile("data.txt");
    int n = 0;
    
    infile >> n;

    std::vector<long long int> vec(n);

    for (auto &i : vec) {
        infile >> i;
    }

    for (auto i : vec) {
        std::cout << i << std::endl;
    }

    return 0;
}

If we use just int, we can check if the read succeeded. How you choose to handle that error is up to you and the requirements of your program.

E.g.

    for (auto &i : vec) {
        if (infile >> i) continue;
        std::cerr << "A problem occurred." << std::endl;
        i = -2;
    }

Now output is:

A problem occurred.
A problem occurred.
A problem occurred.
A problem occurred.
1231234213
12
-2
-2
-2
-2
Sign up to request clarification or add additional context in comments.

1 Comment

this solve the problem (for that input), but doesn't address the main problem in original code: not checking for error while reading from the stream. error will happen again, unhandled, for something such "AB" or "12345678901234567890" in input file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.