I'd like to input 2 dimensional array from text file. But my code doesn't work..
#pragma disable (warnning:c4996)
#include<cstdio>
#include<cstring>
#include<iostream>
#include<fstream>
#include<conio.h>
int main()
{
std::ifstream arrival; std::ifstream departure;
arrival.open("arrival.txt"); departure.open("departure.txt");
int i, j, l = 0;
char temp;
if (arrival.is_open() && departure.is_open()) {
temp = arrival.get();
while (temp != EOF) {
if (temp == '\n')
l++;
temp = arrival.get();
}
}
else
printf("error");
// file read
int** arr = new int*[l];
for (i = 0; i < l; i++)
arr[i] = new int[3];
for (i = 0; i < l; i++) {
for (j = 0; j < 3; j++)
arrival >> arr[i][j];
}
for (i = 0; i < l; i++) {
for (j = 0; j < 3; j++)
printf("%d ", (int)arr[i][j]);
}
_getch();
delete[] arr;
arrival.close();
departure.close();
return 0;
}
this is my code, and
arrival
2018 01 05
2018 02 03
2019 04 03
2019 08 08
2020 07 08
2018 03 28
2018 05 04
2018 08 30
2019 01 06
2019 09 21
2020 02 18
this is the text file.
I surmise in
arrival >> arr[i][j];
this part, it doesn't get value from the file. I have no idea how to solve this problem.