I know this is a widely asked question and there are answers even on this website, unfortunately, none of the answers helped me with my problem which is the following: i open a file and loop over its characters to see how many rows i will need to initialize my dynamic array, then i have another loop to reread the file found out how many columns i have (i know this inst so efficient), then i will input these chars to my 2d dynamic array. We can see here i reread the file several times. I have tried commands like seekg(),rewind(), closing the file and re-opening it none have worked, here is my code : (we have been giving a hint that each row ends with value 0, that should not be counted.)Any help is much appreciated.
//------------- include section -------------
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <new>
#include <fstream>
#include <cstring>
#include <cctype>
//------------- using section -------------
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::setw;
//------------- const section -------------
const int END_OF_LINE=-1;
const int LEN=100;
//------------- prototypes section -------------
void open_files(ifstream& if1, ifstream& if2);
void convert_to_matrix (ifstream &inp,int*matrix[],int num_of_rows);
void add_2_matrixes (int*matrix[],int* matrix2[],int num_of_rows,int
num_of_rows2);
int get_length(ifstream & inp,int &length);
int get_rows(ifstream &inp, int &counter);
//------------- main -------------
int main(int argc, char *argv[])
{
ifstream inp;
ifstream inp2;
open_files(inp,inp2);
int num_of_rows=0,counter=0;
num_of_rows=get_rows(inp,counter);
int* matrix[num_of_rows];
//fill rows and cols dynamically
convert_to_matrix(inp,matrix,num_of_rows);
int num_of_rows2=0;
counter=0;
num_of_rows2=get_rows(inp2,counter);
//another matrix
int* matrix1[num_of_rows2];
//fill rows and cols dynamically
convert_to_matrix(inp2,matrix1,num_of_rows2);
add_2_matrixes(matrix,matrix1,num_of_rows,num_of_rows2);
return EXIT_SUCCESS;
}
//-----------------------------
void open_files(ifstream& inp, ifstream& inp2){
char* st_file;
char* nd_file;
st_file=new(std::nothrow) char [LEN];
nd_file=new(std::nothrow) char [LEN];
//gets the first file name
cin >> setw(LEN) >> st_file;
inp.open(st_file);
//gets the second file name
cin >> setw(LEN) >> nd_file;
inp2.open(nd_file);
// if the opening command failed
if ((!(inp.is_open())) || (!(inp2.is_open()))) {
exit(EXIT_FAILURE);
}
}
//------------------------------------------------
int get_rows(ifstream &inp, int &counter){
int columns=0;
while(!inp.eof())
{
counter++;
inp>>columns;
while (columns!=0)
{
inp>>columns;
}
}
//!!!! (problem)
inp.seekg(0,std::ios::beg);
return counter;
}
//---------------------------------------------
void convert_to_matrix (ifstream &inp,int* matrix[],int num_of_rows){
//numbers is how many cols
int length=0;
//value of each col
int number;
for (int row=0;row<num_of_rows;row++){
//open an array
get_length(inp,length);
matrix[row]=new(std::nothrow) int [length+1];
//if null we failed
if (matrix[row]==NULL)
{
cerr<<"Cannot allocate memory /n";
exit(EXIT_FAILURE);
}
//else start filling in values
for (number=0;matrix[row][number]!=0;number++)
{
inp>>matrix[row][number];
}
//at end of filling values last number is -1
//to help us know when to stop
matrix[row][number]=END_OF_LINE;
}
}
//------------------------------------------------------
int get_length(ifstream &inp,int &length){
int columns=0;
inp>>columns;
length++;
while(columns!=0)
{
inp>>columns;
length++;
}
//!!!!!(problem)
inp.seekg(0,std::ios::beg);
return length;
}