I have a file that I need to read. The file should be read such that once the code reads a number all the following strings goes in to that row of the array. For instance, when the program reads 1, each word that is read following 1, goes in to [1][n]. But when the program reads 2, each word that is read following 2, goes in to [2][n], etc.
I know my code is very rudimentary at best at this point. I am still learning. However, currently my code recognizes the digit. But continues to read each word in to one big 1D array instead of beginning the column count at 1 (or 0) again when a new digit is reached. What should I do to read in the file as a dynamic 2D array, that begins a new row only after a new/different digit is read?
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
// if str contains an unsigned integer, convert and store the value in number,
// and return true. return false otherwise
bool is_unsigned_integer( const std::string& str, std::size_t& number )
{
try
{
// try to convert str to an unsigned integer
// https://en.cppreference.com/w/cpp/string/basic_string/stoul
std::size_t pos ;
number = std::stoul( str, &pos ) ;
return pos == str.size() ; // true if the string was fully consumed
}
// return false on conversion failure
catch( const std::exception& ) {}
returnfalse ;
}
std::vector< std::vector<std::string> > read_data( std::istream& stm )
{
std::vector< std::vector<std::string> > vec2d( 1 ) ; // initially only row zero
std::string str ;
while( stm >> str ) // for each string read
{
std::size_t row_num = 0 ; // keep track of the current row
if( is_unsigned_integer( str, row_num ) ) // if it is a row number
{
// resize the vector if required (so that it has a row row_num)
if( vec2d.size() < (row_num+1) ) vec2d.resize(row_num+1) ;
}
// otherwise, add the string to the end of the current row
else vec2d[row_num].push_back(str) ;
}
return vec2d ;
}
int main()
{
const std::string input_file_name = "whatever.txt" ;
std::ifstream file(input_file_name) ;
constauto vec2d = read_data(file) ;
// ...
}
The file initially looked like
1
seven three two eleven
four nineteen two
five seventeen one five
2
eight six twelve eleven
thirteen twenty two
fifteen fourteen sixteen ten
...
But I removed the endline character to hopefully read the file better. So currently the file that I am working with looks like...
1 seven three two eleven four nineteen two five seventeen one five 2 eight six twelve eleven thirteen twenty two fifteen fourteen sixteen ten...
So now, I am trying to recognize the digit 1 for instance and place "seven three two eleven four nineteen two five seventeen one five" in 1[n]. So that [1][1] points to "seven" and [1][2] points to "three" and so on. Then read 2 and place the "eight" that follows 2 in [2][1] and "six" in [2][2] and so on.
Thanks all...tried all. lastchance's and dutch's worked as listed. The other one, unfortunately still had a 1D array, which is what I already had. I tried tweaking to get to a 2D but couldn't get it to work. Thanks again all.