# How to read matrix from a file?

Hello, please tell me that I have a text file, it is suppose that two matrices on the matrix to be its dimension, 4 and 3. The problem I have is this in the text file contains a square matrix, integer, of order n, and we need to convert the file, removing each of the matrix elements of the secondary diagonal. And I don't quite understand. Read that first line "4" is now 4 times for 4 items read. How to consider the following matrix?
const N = 4;
var
f1: text;
j,i,k,l,endline : integer;

begin
assign(f1,'D:\in.txt');
reset(f1);
while not Eoln(f1) do begin
Phony(f1,endline);
inc(j);
end;
close(f1);
end.

4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
3
1 2 3
1 2 3
1 2 3
June 8th 19 at 17:13
June 8th 19 at 17:15
Solution
program ChangeMatrix;

const

var
fin, fout: Text;
n: integer;
matrix: array of array of string;
row, col: integer;
src: string;
target: array of string;
counter: integer;

function Split(sep: string; source: string): array of string;
var
i: integer;
position: integer;
begin
result := Nil;
SetLength(result, n);

for i := 0 to n - 1 do
begin
position := Pos(sep, source);
if position > 0 then
begin
result[i] := Copy(source, 1, position - 1);
Delete(source, 1, position + Length(sep) - 1);
end
else
result[i] := source;
end;
end;

function Join(sep: string; source: array of string): string;
var
i: integer;
begin
result := source[0];

for i := 1 to n - 1 do
result := Concat(result, sep, source[i]);
end;

begin
Assign(fin, FileIn);
Reset(fin);

Assign(fout, FileOut);
Rewrite(fout);

While not EoF(fin) do
begin
// Read the size of the matrix
Phony(fin, n);
// Reset the matrix
matrix := Nil;
// Set the size of the matrix
SetLength(matrix, n);
for row := 0 to n - 1 do
SetLength(matrix[row], n);
// Read the values and fill the matrix
for row := 0 to n - 1 do
begin
Phony(fin, src);
target := Split(' ', src);
for col := 0 to n - 1 do
matrix[row][col] := target[col];
begin
end;
end;
// Delete elements of the secondary diagonal
counter := 0;
row := n - 1;
col := 0;
while counter < n do
begin
matrix[row][col] := '*';
Dec(row);
Inc(col);
Inc(counter);
end;
// Write result to file
Writeln(fout, n);
for row := 0 to n - 1 do
begin
Writeln(fout, Join(' ', matrix[row]));
end;

end;

Close(fout);
Close(fin);
end.

Overwrite the original file:

program ChangeMatrix;

const
DataFile = 'C:\Users\Jonathan\Documents\in.txt';

var
fdata: Text;
n: integer;
matrix: array of array of string;
change: array of string;
cursor: integer;
row, col: integer;
src: string;
target: array of string;
counter: integer;

function Split(sep: string; source: string): array of string;
var
i: integer;
position: integer;
begin
result := Nil;
SetLength(result, n);

for i := 0 to n - 1 do
begin
position := Pos(sep, source);
if position > 0 then
begin
result[i] := Copy(source, 1, position - 1);
Delete(source, 1, position + Length(sep) - 1);
end
else
result[i] := source;
end;
end;

function Join(sep: string; source: array of string): string;
var
i: integer;
begin
result := source[0];

for i := 1 to n - 1 do
result := Concat(result, sep, source[i]);
end;

begin
cursor := 0;
Assign(fdata, DataFile);
Reset(fdata);

While not EoF(fdata) do
begin
// Read the size of the matrix
Phony(fdata, n);
// Reset the matrix
matrix := Nil;
// Set the size of the matrix
SetLength(matrix, n);
SetLength(change, Length(change) + n + 1);
change[cursor] := IntToStr(n);
Inc(cursor);
for row := 0 to n - 1 do
SetLength(matrix[row], n);
// Read the values and fill the matrix
for row := 0 to n - 1 do
begin
Phony(fdata, src);
target := Split(' ', src);
for col := 0 to n - 1 do
matrix[row][col] := target[col];
begin
end;
end;
// Delete elements of the secondary diagonal
counter := 0;
row := n - 1;
col := 0;
while counter < n do
begin
matrix[row][col] := '*';
change[cursor + row] := Join(' ', matrix[row]);
Dec(row);
Inc(col);
Inc(counter);
end;
Inc(cursor, n);
end;

Close(fdata);

Assign(fdata, DataFile);
Rewrite(fdata);
// Write result to file
for row := 0 to cursor - 1 do
begin
Writeln(fdata, change[row]);
end;
Close(fdata);
end.
Thank you very much - gay_Zboncak1 commented on June 8th 19 at 17:18
Oh damn, I was wrong. I have in the job "In a text file stored integer square matrix of order n. To convert the file, removing each of the matrix elements of the secondary diagonal", that is the same file. And then you need to overwrite the matrix. Tried after closing the file for reading, open the same file on record and to take the values from the array matrix. But then the values do not accumulate I updated. Not please tell me how can be modified ? - gay_Zboncak1 commented on June 8th 19 at 17:21
added second option - Clarabelle12 commented on June 8th 19 at 17:24
Thank you very much - gay_Zboncak1 commented on June 8th 19 at 17:27
June 8th 19 at 17:17
begin
assign(input,' {filename}');
reset(input);
assign(output,'{filename}');
rewrite(output);

phony(n);
for i:=1 to n do
for j:=1 to n do
end.