#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    95
    Rep Power
    11

    list of numbers in a text file into a 2d array


    Hi,
    I have a text file which contains a list of numbers like this
    122
    121
    111
    112
    121
    2
    12
    12121
    21
    21
    212
    1
    I want to read the text file into c++ then create an array with the data that is 121 by 230
    how would I do this?
  2. #2
  3. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1511
    Hmm, homework by the smell of it. Post some of you own code first and then expect help only in very general terms, otherwise expect a moderator to lock the posting.

    Also, where do you get the 121 by 230 from? There's not that much data in the file and I don't see anything in the file to indicate those dimensions.

    Anyway, I will tell you the the generic syntax for declaring a 2 dimensional array of integers is: int MyArray[x][y]; where int is the data type (can be anything, you can even define your own types with the typedef, struct, and class statements), MyArray is the name of the array, and x and y are the sizes of each dimension (in your case 121 and 230).

    To read from a file, you must #include <fstream> or <fstream.H>.
    Delcare a stream like this: fstream mystream;
    Then open the file using that stream: mystream.open ("file.name", ios::beg|ios::nocreate|ios::noreplace);
    The ios:: stuff at the end of the statement are flags you can set. You may want to check your textbook for more info on them. Once the file is open, reading is just like reading in from the keyboard. You would say: mystream >> somevariable; to read in from the file and mystream << somevariableorliteral; to write out to it. You will also find the the .getline(char *s, int l); function comes in handy. It reads a line at a time into a charactar array and you can do what you want with it after that.
    Last edited by f'lar; September 25th, 2003 at 10:07 AM.
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    95
    Rep Power
    11
    Thanks for the help, no its not homework. I am a weather forecaster in the military and trying to createa program for reading forecast weather data. I didn't list every number just an sample to show. That will form a grid of numbers which represent a peice of weather. This one is temperature in kelvin. oh heres my code but I think I am wrong thats why I was asking. I fugured I could get a sample to get me on the right track.
    here is the code I use for reading the file but how do I get that data into an array?
    #include<string>
    #include<iostream>
    #include<fstream>
    #include<vector>
    using namespace std;

    int main ()
    {
    vector<string> v;
    ifstream in ("peloponnesian_a.txt");
    string line;
    while (getline (in, line))
    v.push_back(line); //Add the line to the end //Add line numbers:
    for (int i=0; i<v.size(); i++)
    cout<<i<<": "<<v[i]<<endl;
    }
    //Copy an entire file into a vector of string
  6. #4
  7. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1511
    Sorry, my mistake.

    As for putting the data in an array are you sure you need it? You are already using a vector, which is better. What I would do first is change the vector to type int or long (or maybe float) and read the files using stream operators rather than the getline function (see example below). This will keep the data in a format that is easier to compare, manipulate, etc.

    PHP Code:
    //not really php, i've just discovered the syntax highlighting
    #include <string>
    #include <fstream>
    #include <vector>

    //included array also
    #define X_MAX 121
    #define Y_MAX 230
    double WeatherData[X_MAX][Y_MAX];

    int main () 

    vector<doublev
    double linecount=0
    ifstream fin;
    fin.open ("peloponnesian_a.txt"ios::in|ios::nocreate|ios::beg); 

    fin >> line;
    while (!
    fin.eof() && count Y_MAX)

      
    v.push_back(line); 
      
    WeatherData[0][count]=line;
      
    count++;
      
    fin >> line;
    }  
    fin.close()
    for (
    int i=0i<v.size(); i++) 
    cout<<i<<": "<<v[i]<<endl

    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    95
    Rep Power
    11
    That would work but the data has to be in a 121 bty 230 grid because right now they are like i showed above, but they need to be in that grid to make sence because it is a grid that covers the united states and every grid point represents a longitude and latitude.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    95
    Rep Power
    11
    That program would work great but I cannot no matter what I do get the vector header file to work! something is wrong with the QUATERNION.H file
  12. #7
  13. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1511
    Then don't use the vector. Remove all the code that references it. If you follow the code you will see there is both an array and a vector, and you really only need one or the other. Question though, how is the data organized in the file? You need to know this to read it in to the array. To demonstrate what I'm trying to say, look at these two pieces of code:

    PHP Code:
    const int X_MAX=230Y_MAX=121;
    int count=0linex=0y=0;
    double WeatherData[X_MAX][Y_MAX];
    fin.open ("peloponnesian_a.txt"ios::in|ios::nocreate|ios::beg); 
    fin >> line;
    while (!
    fin.eof() && X_MAX*Y_MAX)

      
    WeatherData[x][y]=line;
      
    x++;
      if (
    x==X_MAX)
      {
        
    y++;
        
    x=0;
      }
      
    fin >> line;
    }  
    fin.close() 
    and

    PHP Code:
    const int X_MAX=230Y_MAX=121;
    int count=0linex=0y=0;
    double WeatherData[X_MAX][Y_MAX];
    fin.open ("peloponnesian_a.txt"ios::in|ios::nocreate|ios::beg); 
    fin >> line;
    while (!
    fin.eof() && X_MAX*Y_MAX)

      
    WeatherData[x][y]=line;
      
    y++;
      if (
    y==Y_MAX)
      {
        
    x++;
        
    y=0;
      }
      
    fin >> line;
    }  
    fin.close() 
    While very similar, there is an important diffence in the two blocks, and while both will probably compile and seem to run, only one will give you accurate results.
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    95
    Rep Power
    11
    it is a straight down list of numbers and I am pretty sure that it should go down 121 then start a new row.

IMN logo majestic logo threadwatch logo seochat tools logo