Nov 15, 2016 at 9:41pm UTC
Hey! I'm trying to get a matrix that is written in a text file (.txt) into a 2 dimensional massif.
I've beed trying to find a way but all I got are not responding's or nothing at all.
1 2 3 4 5 6 7
int p;
for (p=0; !feof(f); p++) c[p] = fgetc(f);
clen=strlen(c);
for (int p=0; p<clen; p++){
for (int i=0; i<clen; i++)
for (int j=0; j<clen; j++)
if (isdigit(c[p])) c[p]=mat[i][j];
Last edited on Nov 15, 2016 at 9:45pm UTC
Nov 16, 2016 at 12:11am UTC
please show your definition of variable c and mat
Nov 16, 2016 at 12:30pm UTC
OP: can you show your .txt file as well
Nov 16, 2016 at 12:43pm UTC
And the matrix you need to read into is 2 x 2 as well?
BTW: what is:
Last edited on Nov 16, 2016 at 12:45pm UTC
Nov 16, 2016 at 12:45pm UTC
It is supposed to create it as big as needed. I mean the .txt file could contain
1 2 3
12 5 0
0 1 1
Then the array created should be 3x3
Last edited on Nov 16, 2016 at 12:54pm UTC
Nov 16, 2016 at 12:49pm UTC
Umm we call it a massif here. I mean I believe it's called an array everywhere else?
Nov 17, 2016 at 2:16am UTC
Sorry, it took me longer than I'd expected to get back to you
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
const int SIZE = 3;
int main()
{
vector<vector<int >> values(SIZE, vector<int >(SIZE)); // your entire data-set of values
ifstream File("F:\\test.txt" );
if (File)
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
File >> values[i][j];
}
}
}
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
cout<< values[i][j] <<"\t" ;
}
cout<<"\n" ;
}
}
Last edited on Nov 17, 2016 at 2:18am UTC