Reading a Matrix Txt File and Storing as an Array

Reading a Matrix txt file and storing as an array

How about this? (KISS solution)

void LoadCities() {
int x, y;
ifstream in("Cities.txt");

if (!in) {
cout << "Cannot open file.\n";
return;
}

for (y = 0; y < 15; y++) {
for (x = 0; x < 15; x++) {
in >> distances[x][y];
}
}

in.close();
}

Works for me. Might not be that complex and perhaps isn't very performant, but as long as you aren't reading a 1000x1000 array, you won't see any difference.

Reading matrix from a text file and storing it in an array

Use np.loadtxt:

A = np.loadtxt('filename.txt')

>>> A
array([[ 1., 0., 0., 1., 0., 1., 1.],
[ 0., 1., 0., 1., 1., 1., 0.],
[ 0., 0., 1., 0., 1., 1., 1.]])

Alternatively, you could read it line by line similarly to what you were doing (but this isn't efficient):

A = []
with open('filename.txt', 'r') as f:
for line in f:
A.append(list(map(int,line.split())))

>>> np.array(A)
array([[1, 0, 0, 1, 0, 1, 1],
[0, 1, 0, 1, 1, 1, 0],
[0, 0, 1, 0, 1, 1, 1]])

Reading Matrix from file

A good old pure python algorithm (assuming matrices can hold string values, otherwise, convert as required):

file = open("inputs.txt",'r')
matrices=[]
m=[]
for line in file:
if line=="1 1\n":
if len(m)>0: matrices.append(m)
m=[]
else:
m.append(line.strip().split(' '))
if len(m)>0: matrices.append(m)
print(matrices)
# [[['0', '0', '0', '0'], ['0', '0', '0', '0'], ['0', '0', '0', '0'], ['0', '0', '0', '0']],
# [['0', '0', '0', '0'], ['0', '0', '0', '0'], ['0', '0', '0', '0'], ['0', '0', '0', '0']],
# [['0', '0', '0', '0'], ['0', '0', '0', '0'], ['0', '0', '0', '0'], ['0', '0', '0', '0']]]

Read 2d array from txt file in c++

There are a lot of other ways to perform the specific task, but i guess your method is not wrong, and you have just made a simple typing mistake in your second for loop condition. so ill just fix your code for you.
and also you could just input single values at a time as u go.

#include<bits/stdc++.h>
using namespace std;
int main()
{
char arr1[10][10];
cout <<"Reading Start" <<endl;
ifstream rfile("test.txt");
int i,j;
for(i=0;i<6;i++){
for(j=0;j<6;j++){
rfile >> arr1[i][j];
cout << arr1[i][j] << " ";
}
cout << endl;
}

cout <<"\nRead Done" <<endl<<endl;
rfile.close();
}

Output : Sample Image

How to read a matrix from text file and store it into 2D array?

I think something like this works

#include <iostream>
#include <fstream>

int main() {

const int nSize = 50;
//-- initialize array with 0 --
char map[nSize][nSize] = { {0} };

std::ifstream in;
in.open("input.txt");

int i = 0, j = 0;
while (!in.eof()) {
char next = in.get();
if (next != '\n')
map[i][j++] = next;
else {
j = 0;
i++;
}
}

int rowsCount = i + 1;
for (i = 0; i < rowsCount; i++) {
j = 0;
while (map[i][j] != 0)
std::cout << map[i][j++];
std::cout << std::endl;
}

return 0;
}

All lines of text ends with "end line symbol" '\n' or '\r\n'. This can signalize to go to new row of chars in array.
Since array was initialized with 0, we can use it as flag of end of row in output, but better would be calculate size of array while reading it (if all lines have same size).



Related Topics



Leave a reply



Submit