Read ASCII file into matrix in C++

How to read and write ascii file

Page 1 of 1

10 Replies - 8979 Views - Last Post: 04 November 2010 - 04:36 PM Rate Topic: -----

#1 abotaha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-November 10

Read ASCII file into matrix in C++

Posted 02 November 2010 - 07:34 AM

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 data is attached.

Attached File(s)


Is This A Good Question/Topic? 0
  • +

Replies To: Read ASCII file into matrix in C++

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,753
  • Joined: 25-December 09

Re: Read ASCII file into matrix in C++

Posted 02 November 2010 - 07:40 AM

What have you tried. Please post some code that shows what you don't understand.

Please use [ code] code blocks [/code].

Also if the code does not compile cut/paste the entire error message.

EDIT: Please don't crosspost.

Jim

This post has been edited by jimblumberg: 02 November 2010 - 07:45 AM

Was This Post Helpful? 0
  • +
  • -

#3 abotaha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-November 10

Re: Read ASCII file into matrix in C++

Posted 02 November 2010 - 07:51 AM

Thanks jimblumberg for the reply,
I want to read the ascii file and store the data in a matrix. i do not know how i can do that, but i can read the data as in example below:
 FILE *stream;
char filename[100];

    sprintf(filename, "onlyTower.txt", input_path,Class_num);
    stream = fopen(filename, "r");
    if( stream == NULL )
    {
        printf( "Error! Input image was not opened\n" );
        return -1;
    }


Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,753
  • Joined: 25-December 09

Re: Read ASCII file into matrix in C++

Posted 02 November 2010 - 08:01 AM

Is this a C or C++ program?

Have you looked through the this sites Tutorials?

The above fragment just opens the file using "C", although the sprintf() does not appear correct.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,005
  • Joined: 30-May 10

Re: Read ASCII file into matrix in C++

Posted 02 November 2010 - 09:56 AM

Are you meant to extract the size information out of the comment lines at the start?

Typically, I would suggest you use fgets() to read each line, then process the line.

char buff[BUFSIZ];
while ( fgets( buff, sizeof buff, fp ) != NULL ) {
  // do stuff
}



Then say
if ( buff[0] == ';' )



For walking along a line, converting numbers, then look at using strtod()
Was This Post Helpful? 0
  • +
  • -

#6 abotaha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-November 10

Re: Read ASCII file into matrix in C++

Posted 03 November 2010 - 03:35 AM

Hello everybody

This time I have a code which can read an ascii file and sort the data file to a matrix. I am looking forward for more suggestions if the code needs more improvement.

The code is:
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main ()
{
    string line;
    int rows,cols;
    float my_array[4][4];
    ifstream pFile ("asciifile.txt");
    if (pFile.is_open())
    {
        rows=0;
        while(!pFile.eof())
        {
            getline(pFile, line);
            stringstream ss(line);
            cout<<rows<<"\n";
            cols=0;
            cout<<col<<"\n";
            while(ss >> my_array[rows][cols])
            {
                cols++;
            }
            rows++;
        } 
        pFile.close();
    }
    else 
        cout << "Unable to open file"; 
    for(int i=0;i<rows;i++)
    {
        for(int j=0;j<cols;j++)
        {
            cout<<my_array[i][j]<<"\t";
        }
        cout<<"\n";
    }
    system("pause"); 
    return 0;
}



The ascii file is
12	23	45    87.4	
32	12	21    76.2
34	56	78    43.6
98	76	54    -76


This post has been edited by abotaha: 03 November 2010 - 03:37 AM

Was This Post Helpful? 0
  • +
  • -

#7 Guest_abotaha*


Reputation:

Re: Read ASCII file into matrix in C++

Posted 03 November 2010 - 01:12 PM

Hello again
After I have solved my last challenge. I got another challenge. This time 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.
more information about FLT file is FLT file.
Was This Post Helpful? 0

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Read ASCII file into matrix in C++

Posted 03 November 2010 - 04:10 PM

Show us the code that is your best try at doing this.

If your best try does not work then provide a test sample FLT file that you are testing against (so we can test against it too).

Post your best attempt in code tags, like this :code:
and answer these questions
( a ) Does your code compile?
( b ) Any errors or warnings? If there are then share them with us.
Ideally copy and paste the errors exactly as they are.
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect?
Give details and, ideally, examples.
If you provided inputs to the program tell us what they were.
( e ) What have you already tried to fix it?
Was This Post Helpful? 0
  • +
  • -

#9 abotaha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-November 10

Re: Read ASCII file into matrix in C++

Posted 04 November 2010 - 12:44 AM

Hi,
I used the same code that reads the ascii file (the code above) to read the flt file. Anyhow the code is:
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main ()
{
    string line;
    int rows,cols,r=3,c=3;
    float array01[r][c];
    ofstream writedata;
    ifstream file01;
    file01.open("float3by3.flt",ios::binary);
	if (file01.is_open())
    {
        rows=0;
        while(!file01.eof())
        {
            getline(file01, line);
            stringstream ss(line);
            cout<<rows<<"\n";
            cols=0;
            cout<<cols<<"\n";
            while(ss >> array01[rows][cols])
            {
                cols++;
            }
            rows++;
        } 
        file01.close();
    }
    else 
        cout << "Unable to open file"; 
		for(int i=0;i<rows;i++)
    {
        for(int j=0;j<cols;j++)
        {
            cout<<array01[i][j]<<"\t";
        }
        cout<<"\n";
    }
    cout<<"\n";
    cout<<array01[0][2]<<"\n";
	system("pause"); 
    return 0;
}



I used an example of flt file which contains data as a matrix of 3 by 3.
Please find the example float3by3.rar.
The output of the code was strange (only zeros). I am not sure if i need to change the code bit in order to read the flt file.

Help please......
Was This Post Helpful? 0
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Read ASCII file into matrix in C++

Posted 04 November 2010 - 12:56 AM

What output did you expect?
How is the output you got different to the output you expected?
This is your program and your bug. Do the work to tell us what is wrong.

--------------------------------
On another subject
What happens in your code if you fail to open the file?
Do you still try to do work? Yes you do.
Why would you do work if the file didn't open?
Was This Post Helpful? 0
  • +
  • -

#11 abotaha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-November 10

Re: Read ASCII file into matrix in C++

Posted 04 November 2010 - 04:36 PM

Hello, Now after help from friends and spending some time of reading and working i ended up with the following code which reads a flt file and store the data in a matrix as follows:-

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




I feel that the vector is not needed and i want to avoid it and store the data in matrix direct but i do not know how to do that in a right way.

looking forward to your help.

This post has been edited by abotaha: 04 November 2010 - 04:38 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1