reading an ASCII file

cpp Mexico
  • 18 years ago

    hi!
    i'm beginner in c++ and i am trying to read an ASCII file:
    the file has the following structure:


          0        17  
    10.125  -85.189      
    10.115  -85.181      
    10.106  -85.171      
     10.1  -85.158      
     10.1  -85.151      
    10.097  -85.138  
    ......


    how can i read this file into a matrix with 2 columns and "n"rows?
    thanks!
    marina

  • 18 years ago

    Code:
    #include <fstream>
    using std::ifstream;
    using std:fstream;


    include <iostream>


    using std::cout;
    using std::cin;
    using std::ios;


    void main()
    {
      float List[100][2];
      int x = 0;


      ifstream inFile("InputFile.txt", ios::in);   // Input File Stream


      // Loop until end of file
      while (inFile.eof() == false)
      {
         cin >> List[x][0] >> List[x][1];   // Input the first and second columns into row x
         x++;   // Increment your counter
      }


      // Close file
      inVideoFile.close();
    }



    That should work.  If you have any problems, just respond and I'll try to be prompt with my reply.  Good luck!

  • 18 years ago

    Thank you!
    (i was out of my office for a week). i will try this

    marina

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” - Tom Cargill