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

P.S. the ascii file is attached.

Attachments
;
; 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
        0.8520        0.8561        0.8561        0.8474        0.8395        0.8049        0.7857        0.8036        0.8235        0.8275        0.8394        0.8367        0.8462        0.8286        0.8356        0.8356        0.8373        0.8333        0.8373        0.8509        0.8625        0.8292        0.7883        0.8040        0.8545        0.8509        0.8496        0.8235        0.8235        0.8395        0.8117        0.8144        0.8118        0.8333        0.8065        0.8067        0.8338        0.8212        0.8212        0.8471        0.8338        0.8338        0.8590        0.8359        0.8162        0.8142        0.8201        0.8292        0.8478        0.8338        0.8359        0.8625        0.8667        0.8505        0.8329        0.8248        0.8289        0.8512        0.8667        0.8478        0.8734        0.8575        0.8544        0.8347        0.8289        0.8378
        0.8466        0.8640        0.8640        0.8529        0.8448        0.8118        0.8301        0.8144        0.8212        0.8351        0.8351        0.8310        0.8118        0.8092        0.8351        0.8462        0.8395        0.8310        0.8351        0.8351        0.8457        0.8269        0.7818        0.7852        0.8367        0.8286        0.8235        0.7985        0.8118        0.8141        0.8041        0.8169        0.8188        0.8373        0.8144        0.8144        0.8117        0.8457        0.8204        0.8297        0.8280        0.8318        0.8318        0.8248        0.8226        0.8289        0.8393        0.8324        0.8471        0.8471        0.8416        0.8545        0.8640        0.8535        0.8428        0.8529        0.8430        0.8615        0.8667        0.8550        0.8653        0.8505        0.8428        0.8428        0.8601        0.8378
        0.8509        0.8772        0.8512        0.8586        0.8343        0.8117        0.8117        0.8067        0.8212        0.8286        0.8286        0.8286        0.8286        0.8286        0.8333        0.8333        0.8478        0.8440        0.8310        0.8118        0.8373        0.8436        0.8067        0.7983        0.8247        0.8015        0.8065        0.8235        0.8188        0.8020        0.8310        0.8188        0.8188        0.8310        0.8169        0.8356        0.8118        0.8436        0.8318        0.8235        0.8235        0.8491        0.8280        0.8498        0.8395        0.8289        0.8338        0.8491        0.8235        0.8169        0.8416        0.8430        0.8550        0.8643        0.8643        0.8535        0.8538        0.8664        0.8626        0.8550        0.8505        0.8505        0.8445        0.8393        0.8561        0.8512
        0.8436        0.8667        0.8615        0.8734        0.8592        0.8280        0.8067        0.8117        0.8188        0.8333        0.8118        0.8351        0.8286        0.8373        0.8351        0.8261        0.8310        0.8310        0.8065        0.8188        0.8188        0.8478        0.8478        0.8417        0.8118        0.7923        0.8333        0.8333        0.8020        0.8118        0.8310        0.8144        0.8261        0.8092        0.8169        0.8356        0.8092        0.8188        0.8047        0.8188        0.8235        0.8471        0.8258        0.8462        0.8440        0.8557        0.8333        0.8189        0.7752        0.8216        0.8333        0.8563        0.8714        0.8849        0.8616        0.8638        0.8484        0.8565        0.8550        0.8535        0.8520        0.8490        0.8466        0.8496        0.8544        0.8544
        0.8436        0.8545        0.8529        0.8560        0.8529        0.8362        0.8067        0.8117        0.8188        0.8188        0.8118        0.8092        0.8092        0.8373        0.8144        0.8261        0.8333        0.8310        0.8092        0.8258        0.8188        0.8395        0.8373        0.8333        0.8310        0.8351        0.8118        0.8144        0.8163        0.8212        0.8333        0.8261        0.8261        0.8169        0.8144        0.8333        0.8188        0.8188        0.8310        0.8188        0.8297        0.8457        0.8144        0.8161        0.8132        0.8261        0.7891        0.7983        0.8167        0.8462        0.8333        0.8528        0.8785        0.8750        0.8575        0.8602        0.8462        0.8505

That file contains some comments at the top, then four colums of data. There are several ways to read the data, one of them might be to create a structure with 4 data items representing each of the 4 columns of data, then put that structure into a vector for each row in the file

struct data
{
   double col1, col2, col3, col4;
};

vector<data> rows;

Now write your program to skip all the lines that start with a semicolon or are blank, then use >> ifstream operator to read 4 data items into the structure and push the structure onto the vector.

I'm being a little vague about that on purpose -- I'm not going the write the program for you.

Edited 6 Years Ago by Ancient Dragon: n/a

Thanks a lot for this explanation. As you said it is vague and especially for the beginner one like.
i highly appreciatory your time if you could provide a short c++ code, just give me movement from the first step.

Cheers

Edited 6 Years Ago by havalka: n/a

Ok, here's a start. Now all you have to do is take this template and fill it in with the code you write. If you have not yet learned about ifstream and vector then you will need to study up on them. Every c++ textbook worth its salt will contain the information you need, or you can use google to easily find it online.

#include <fstream>
#include <iostream>
#include <vector>

using std::ifstream;
using std::cin;
using std::cout;
using std::vector;

int main()
{
   // put your code here

}

Edited 6 Years Ago by Ancient Dragon: n/a

Thanks a lot......i will see what i can do then.

thanks once again

Hi Ancient Dragon,
This time I have code to read the ascii file and save the data in a matrix, but i still have problem that the data does not save in the matrix at the same position as in the ascii file.
The code is:

#include <fstream>
#include <iostream>
#include <stdio.h>
#include <sstream>
using namespace std;
int main ()
{
string line;
int row,col;
float my_array[2][3];
  ifstream pFile ("yyy.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";
  cout<<my_array[0][2]<<"\n";
  system("pause"); 
  return 0;
}

and the yyy ascii file is

5.1 5.3 5.5 5.7
6.1 6.3 6.5 6.7
7.1 7.3 7.5 7.7

Any help would appreciate!!...

Edited 6 Years Ago by havalka: n/a

Your program was difficult to follow because it was formatted so poorly. Don't be afraid to use lots of white space to make it easier to understand.

The problem is that the array does not contain enough rows and columns -- there are four columns in the data file but only 3 in the array.

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main ()
{
    string line;
    int row,col;
    float my_array[2][3];
    ifstream pFile ("yyy.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";
    cout<<my_array[0][2]<<"\n";
    system("pause"); 
    return 0;
}

Edited 6 Years Ago by Ancient Dragon: n/a

Thanks a lot.....I will take your consideration and notes for further work.
kiss to you from a pretty lady :-)

Cheers

havalka

Hello Ancient Dragon,
I have a very difficult challenge. This time I want to read a flt file (float). My data is stored in flt file. The code that I built is only to read a text file but I do not have any idea of how I can read flt file.
I have searched in internet about this case but I could not find any useful information.
could you please help me and modify the code to read a flt file.

cheers,

I don't understand the problem -- the code you wrote already reads floats. Or are you asking how to read a file where the floats were written as binary data instead of text? That all depends on how the file was written -- you will have to post the program that wrote it.

The problem that the output of the code after it reads the flt file is wrong. The flt is the file extension for the ArcInfo Float Grid format. It is a binary file which is used together with an ASCII header file with extension HDR. Here http://www.coolutils.com/Formats/FLT is more information about flt file.
Please find the example (float3by3.rar) of flt file from this link http://www.2shared.com/file/0lZtd1Kq/float3by3.html.
This flt file contains a data as a matrix 3 by 3. but the output was only zeros.
Please I need your help.

Edited 6 Years Ago by havalka: n/a

Here you go

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    float n;
    vector<float> data;
    ifstream in("float3by3.flt", ios::binary);
    if( in.is_open() )
    {
        while( in.read((char *)&n, sizeof(float)) )
            data.push_back(n);
    }
    // just display the data
    vector<float>::iterator it;
    for(it = data.begin(); it != data.end(); it++)
        cout << *it << '\n';
}

Thanks a lot, you helped me a lot and made me learn C++. I know it may still simple things for you, but for me it is too much.
Now the code to read a flt file and to save it as a matrix is:-

#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); 
    }
    // Save 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;
}

Am I right!!!!????

Edited 6 Years Ago by havalka: n/a

This article has been dead for over six months. Start a new discussion instead.