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.

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.

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

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

}

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!!...

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;
}

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.

Please help......

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!!!!????

There is no need for that vector in your program. Remove it and store the data directly into the matrix.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.