Trying to get numbers from text file

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
please show your definition of variable c and mat
1
2
float **mat;
char* c[MAX]="";
OP: can you show your .txt file as well
1 2
3 4
And the matrix you need to read into is 2 x 2 as well?

BTW: what is:

a 2 dimensional massif
Last edited on
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
Umm we call it a massif here. I mean I believe it's called an array everywhere else?
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
Topic archived. No new replies allowed.