Read ASCII file into matrix in C++

Hello everybody,

I am new to this forum. I got a stuck in my work ... I have ASCII file with numerical values, and I need to read them and save them in a matrix of 2 dimensions?.... If anyone can help me please ....!!???

Looking forward to any help
Cheers

the ascii data looks like
;
; ENVI ASCII Output of file: [Memory1] (66x68x1) [Tue Nov 02 11:45:08 2010]
; File Dimensions: 66 samples x 68 lines x 1 band
; Line Format    : (66f14.4)
;
        0.8442        0.8343        0.8163        0.8261        0.8235        0.8188        0.8204        0.8364        0.8571        0.8490        0.8505        0.8483        0.8528        0.8338        0.8373        0.8462        0.8417        0.8440        0.8538        0.8519        0.8625        0.8471        0.8529        0.8586        0.8720        0.8512        0.8436        0.8356        0.8356        0.8333        0.8483        0.8462        0.8462        0.8092        0.7759        0.7410        0.8000        0.7993        0.8067        0.8436        0.8047        0.8165        0.8182        0.8117        0.8117        0.8261        0.7985        0.8416        0.8362        0.8188        0.8280        0.8489        0.8411        0.8411        0.8258        0.7818        0.7714        0.7436        0.7816        0.8393        0.8381        0.8601        0.8484        0.8364        0.8505        0.8478
        0.8428        0.8343        0.8188        0.8286        0.8261        0.8132        0.8204        0.8380        0.8462        0.8602        0.8589        0.8483        0.8652        0.8496        0.8436        0.8462        0.8498        0.8373        0.8373        0.8333        0.8519        0.8576        0.8544        0.8586        0.8734        0.8560        0.8563        0.8356        0.8440        0.8498        0.8557        0.8333        0.8333        0.8212        0.7507        0.7855        0.8118        0.8067        0.8297        0.8117        0.8373        0.8141        0.8235        0.8169        0.7898        0.8258        0.8356        0.8430        0.8393        0.8398        0.8141        0.8448        0.8393        0.8238        0.8380        0.8304        0.7428        0.6972        0.7799        0.8462        0.8065        0.8484        0.8561        0.8505        0.8520        0.8462
        0.8442        0.8269        0.8269        0.8118        0.8327        0.8327        0.8118        0.8116        0.8428        0.8589        0.8638        0.8586        0.8512        0.8448        0.8395        0.8235        0.8333        0.8373        0.8169        0.8356        0.8457        0.8430        0.8529        0.8560        0.8720        0.8571        0.8667        0.8528        0.8576        0.8436        0.8519        0.8395        0.8498        0.8373        0.8117        0.8188        0.8356        0.8182        0.8226        0.8258        0.8188        0.7993        0.7993        0.7927        0.8329        0.8478        0.8393        0.8362        0.8411        0.8235        0.8436        0.8398        0.8269        0.8085        0.8311        0.8529        0.8092        0.8067        0.8165        0.8329        0.7895        0.8027        0.8362        0.8462        0.8490        0.8364
        0.8513        0.8364        0.8235        0.8103        0.8161        0.7923        0.7840        0.8304        0.8244        0.7982        0.7816        0.8182        0.8182        0.8466        0.8457        0.8417        0.8216        0.8310        0.8356        0.8457        0.8430        0.8380        0.8544        0.8696        0.8720        0.8586        0.8720        0.8667        0.8652        0.8412        0.8483        0.8519        0.8457        0.8338        0.8235        0.8258        0.8258        0.8297        0.8117        0.8235        0.8141        0.8161        0.8212        0.8292        0.8426        0.8544        0.8411        0.8483        0.8512        0.8412        0.8457        0.8226        0.8378        0.8411        0.8601        0.8347        0.8362        0.8362        0.8445        0.8278        0.8380        0.7901        0.8289        0.8362        0.8529        0.8430
        0.8513        0.8520        0.8297        0.8216        0.7752        0.7857        0.7754        0.7947        0.7755        0.7347        0.7460        0.7976        0.8430        0.8457        0.8483        0.8498        0.8310        0.8416        0.8576        0.8545        0.8640        0.8529        0.8720        0.8586        0.8571        0.8544        0.8571        0.8544        0.8560        0.8466        0.8462        0.8235        0.8235        0.8297        0.8395        0.8117        0.8182        0.8457        0.8182        0.8318        0.8165        0.8417        0.8471        0.8324        0.8381        0.8311        0.8428        0.8529        0.8601        0.8560        0.8592        0.8280        0.8457        0.8496        0.8426        0.8411        0.8278        0.8478        0.8535        0.8478        0.8445        0.8380        0.8398        0.8509        0.8416        0.8356
        0.8578        0.8575        0.8512        0.8483        0.8092        0.7923        0.7687        0.7778        0.7791        0.7414        0.7913        0.7985        0.8483        0.8333        0.8333        0.8373        0.8356        0.8519        0.8395        0.8528        0.8652        0.8490        0.8234        0.8182        0.8529        0.8681        0.8571        0.8496        0.8436        0.8417        0.8373        0.8216        0.8118        0.8118        0.8356        0.8118        0.8608        0.8359        0.8182        0.8235        0.8280        0.8457        0.8471        0.8338        0.8182        0.8182        0.8201        0.8292        0.8458        0.8483        0.8529        0.8590        0.8509        0.8496        0.8394        0.8218        0.8380        0.8347        0.8535        0.8520        0.8544        0.8411        0.8496        0.8304        0.8235        0.8356
Read each line using getline() into a string. Use a stringstream to extract each item in the rows and then assign them to individual elements in the matrix.
Thank you for the reply,
could you please show me a simple example to do that.

thanks
What he means was
1
2
3
4
5
6
7
8
9
10
11
int row = 0;
while(!myfile.eof()){
   std::string str;
   std::getline(myfile, str);
   //if(str[0] == ';') continue //for comments
   std::stringstream ss(str);
   int col = 0;
   while(ss >> my_array[row][col]) col++;

   row++;
}
WoW....this is amazing!!!!:-)
I have think in that way,,,,,i was thinking in too complicated code.
thanks a lot hamsterman.

Hello guys,

after I learned C++ bit and I followed your suggestions. I wrote my code. It works but not like what I need, I cannot understand why. Since I need to read an ASCII numerical file and save data in matrix of two dimensions. Any more suggestions please.
The code is:
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 <fstream>
#include <iostream>
#include <stdio.h>
#include <sstream>
using namespace std;
int main ()
{
string line;
int row,col;
float my_array[4][3];
  ifstream pFile ("Data.txt");
  if (pFile.is_open())
  {
  while(!pFile.eof())
  {
  getline(pFile, line);
  stringstream ss(line);
  col=0; row=0;
  while(ss >> my_array[row][col])
  col++;
  row++;
  } 
  pFile.close();
  }
  else cout << "Unable to open file"; 
  for(int i=0;i<row;i++)
  {for(int j=0;j<col;j++){
  cout<<my_array[i][j]<<"\t";}
  cout<<"\n";}
  system("pause"); 
  return 0;
}


and the example ascii file is:
1
2
3
4
12	23	45	
32	12	21
34	56	78
98	76	54


So I want to get a matrix of two dimensions but I cannot see that in the output, where after I run the code above with example of ascii file I got the last row only and as following
98     76     54
Hello again,

I have solved the problem but I highly appreciate if there are more suggestions which make the code better in terms of programming. and help me to improve my programming skills.
The code is:
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
33
34
35
36
37
38
39
40
41
42
43
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main ()
{
    string line;
    int row,col;
    float my_array[4][3];
    ifstream pFile ("Data.txt");
    if (pFile.is_open())
    {
        row=0;
        while(!pFile.eof())
        {
            getline(pFile, line);
            stringstream ss(line);
            cout<<row<<"\n";
            col=0;
            cout<<col<<"\n";
            while(ss >> my_array[row][col])
            {
                col++;
            }
            row++;
        } 
        pFile.close();
    }
    else 
        cout << "Unable to open file"; 
    for(int i=0;i<row;i++)
    {
        for(int j=0;j<col;j++)
        {
            cout<<my_array[i][j]<<"\t";
        }
        cout<<"\n";
    }
    cout<<"\n";
    system("pause"); 
    return 0;
}
Last edited on
Hello
I have another challenge which I really do not know how to solve it.
The code above reads the ascii file, but now i want to read the FLT file. This is to difficult for me to figure out.
Please if the there is any suggestion would be appreciated.
The FLT file is a Float Grid format.

looking forward to your help.
You have to find information about what that file is supposed to contain.
Quite likely there will be some sort of header and the array of floats.
see http://www.cplusplus.com/doc/tutorial/files/ (the part about binary files)
basically myfile >> my_float; becomes myfile.read((char*)&my_float, sizeof(float));
Your line 15 is not quite right
 
while(!pFile.eof())

The EOF flag is not set until *after* the read fails. So you will process one line of bad data using that. It is much better to put your file read operation in the while clause if possible like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
        // getline() returns the stream. testing the stream with while returns error such as EOF.
        while(getline(pFile, line)) 
        {
            // so here we know that the read was a success and that line has valid data
            stringstream ss(line);
            cout<<row<<"\n";
            col=0;
            cout<<col<<"\n";
            while(ss >> my_array[row][col])
            {
                col++;
            }
            row++;
        } 
Last edited on
Hello guys,

Thanks for your helps and suggestions. After I followed your suggestions and i got help from friend, I ended up with the following code which reads a flt file and store the data as a matrix of 2 D as follows:-
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
33
34
35
36
37
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    float n;
    int row,col;
    float my_array[3][3];
    vector<float> data;
    ifstream in("float3by3.flt", ios::binary);
    if( in.is_open() )
    {
        row=0;
        while( in.read((char *)&n, sizeof(float)) )
            data.push_back(n); 
    }
    // Store the data in a matrix and display them
    vector<float>::iterator it;col=0;row=0;
    for(it = data.begin(); it != data.end(); it++)
    {
       my_array[row][col]=*it;
       cout << my_array[row][col] << "\t";
       col++;
       if(col==3)
       {
           cout<<"\n";
           col=0;
           row++;
       }
    }
    system("pause"); 
    return 0;
}


However, your contribution to improve the code is most welcome and would be appreciated.
Looking forward to more suggestions,

Cheers,

abotaha
Topic archived. No new replies allowed.