0

How do you read in values in file in to 2D array

19th Apr 2019, 5:02 PM
Lethabo Master Lee
4 Answers
+ 1
Which lang are u using?
20th Apr 2019, 4:25 AM
LetterC67
LetterC67 - avatar
+ 1
Suppose there is 'file.txt' 1 2 3 4 5 6 7 8 9 Use this code #include <iostream> #include <fstream> //to read data from file int main(){ std::ifstream ifs ("file.txt"); // open file to read int arr[10][10]; if(!ifs) Check if file is open std::cout<<"Cannot open file"; else{ for(int i=0;i<3 /*row*/;i++){ for(int j=0;j<3/*col*/;j++){ ifs >> arr[i][j]; std::cout<<arr[i][j]<<' '; } std::cout<<std::endl; } } ifs.close(); // close file return 0; }
20th Apr 2019, 11:43 AM
LetterC67
LetterC67 - avatar
+ 1
Thanks a lot 👌👌
20th Apr 2019, 6:38 PM
Lethabo Master Lee
0
C++
20th Apr 2019, 10:41 AM
Lethabo Master Lee