0
How do you read in values in file in to 2D array
4 Answers
+ 1
Which lang are u using?
+ 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;
}
+ 1
Thanks a lot đđ
0
C++