-1

I want to read a file in 2d array. Basically File is like this

abcedf
ghijkl
mnoqre

Now i know the rows of files but i don't know the columns but col numbers are same for all rows. Now if i do this.

 for (int i = 0; i < row_size; i++){

 fin>>value;
 row[i]=growfunction 
 }

With this program will keep adding all file lines to first row because there is no condition so i can make it move to second row. What can i do?

Thank You for your time.

Deanmon
  • 17
  • 3

1 Answers1

1

The best approach would be to directly read a whole line into a string and then use std::strstream to input the elements of that line into individual values in arrays.

#include <iostream>
#include <sstream>

int main(int argc, char* argv[]) {

  std::string line;
  while(std::getline(std::cin, line)) {
    int rowSize = line.size();
    char * row = new char[rowSize];
    std::strringstream strm;
    strm << line;
    for (int i = 0; i < rowSize; ++i) {
      strm >> row[i];
    }
  }
}
vavasthi
  • 922
  • 5
  • 14
  • Is it good to teach people use C-style strings and the deprecated (i.e. should not be used) `std::strstream`? – L. F. Jan 27 '19 at 12:08
  • Agreed, please make following changes `#include` to `#include ` and std::strstream to std:stringstream. – vavasthi Jan 27 '19 at 12:18