简体   繁体   中英

Putting text file content into a 2D array

I'm trying to read in a text file that is 200 x 1000 of numbers into an array. Each number is separated by a tab. I thought using a 2D array would be good for this situation since it would allow me to distinguish between the individual rows. Being able to distinguish between the individual rows is important in this situation which is why I wanted to do it in this manner. Right now I have the following:

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

using namespace std;

int main()
{
    ifstream file("text.txt");
    if(file.is_open())
    {
        string myArray[1000];

        for(int i = 0; i < 1000; ++i)
        {
            file >> myArray[i];
            cout << myArray[i] << endl;
        }
    }
}

Which currently scans the first row of numbers into the array and then prints it out. I wanted to have a 2D array that scans each individual row into the array but keeps them separate. This means the content of row 1 is separate from the content of row 2. I figured a 2D array would do this. I am a bit stuck on this part. I attempted to make a 2D array through the use of a nested for loop but when I tried to copy the values over into the array things started to go wrong. The order was incorrect and the rows were not separated. I am coding this in C++. If someone could help me understand how to import a text document like the one I described and send all that information to a 2D array I would greatly appreciate it. Thanks.

You can use a stringstream to extract the numbers from each row. Also, you don't need to have an array of string s. You can just use one string .

int main()
{
    ifstream file("text.txt");
    if(file.is_open())
    {
        for(int i = 0; i < 1000; ++i)
        {
            string row;
            if ( std::getline(file, row) )
            {
               std::istringstream istr(row);
               int number;
               while ( istr >> number )
               {
                  // Add the number to a container.
                  // Or print it to stdout.
                  cout << number << "\t";
               }
               cout << endl;
            }
        }
    }
}

If your dimensions change, you use the following code. Note that you can use std::copy to copy from a std::stringstream into a std::vector :

#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>

int main()
{
  std::string input =
    "11 12\n"
    "21 22\n"
    "31 32\n"
    "41 42\n"
    ;

  std::stringstream file(input);

  std::string temp;
  while (std::getline(file, temp)) {

    std::stringstream line(temp);

    std::vector<std::string> v;
    std::copy(
        std::istream_iterator<std::string>(line),
        std::istream_iterator<std::string>(),
        std::back_inserter(v));

    for (auto x: v)
      std::cout << x << " ";

    std::cout << "\n";

  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM