Hello, I'm working on an assignment where we are using file i/o. The goal of the assignment is to read numbers that are seperated by commas in a 2 column x 20 row excel file. I am currently stuck on how to get the numbers out of the file and into the proper sized array. I am ultimately supposed to change the file from seperated by commas to seperated by tab. Here is what I have so far.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
typedef int* intArrayPtr;
int main ()
{
int numberOfColumns = 1; //always at least 1 column
int numberOfRows = 0;
int i = 0;
int i2 = 0;
string line;
string getNumbers;
int numbers[100];
string checkColumns;
ifstream inStream;
inStream.open("C201_test_file.csv");
if(inStream.fail())
{
cout << "Input file opening failed." << endl;
exit(1);
}
getline(inStream, checkColumns); //check number of columns
for(i = 0;checkColumns[i] != '\0'; ++i)
{
if(checkColumns[i] == ',')
++numberOfColumns;
}
inStream.seekg(0, ios::beg); //start over at beginning of file
i = 0;
while(! inStream.eof()) // check number of rows
{
getline(inStream, line);
++i;
}
numberOfRows = i - 1;
inStream.seekg(0, ios::beg);
inStream.seekg(0, ios::beg);//start over at beginning of file again
i = 0;
while(! inStream.eof()) // check number of rows
{
getline(inStream, getNumbers, ',');
istringstream buffer(getNumbers);
buffer >> numbers[i];
++i;
}
intArrayPtr *arrayNumbers = new intArrayPtr[numberOfRows];
for(i = 0; i < numberOfRows; i++) //initialize multidimensional array
arrayNumbers[i] = new int[numberOfColumns];
for(i = 0; i < numberOfRows; i++)
for(i2 = 0; i2 < numberOfColumns; i2++)
arrayNumbers[i][i2] = numbers[i];
for(i = 0; i < numberOfRows; i++) //display multidimensional array
{
for(i2 = 0; i2 < numberOfColumns; i2++)
cout << arrayNumbers[i][i2] << " ";
cout << endl;
}
inStream.close();
return 0;
}
The problem I am having is that I cannot figure out how to get the numbers out of the file and into an array. The numbers in the excel file are 1-20 in the first column and 10-29 in the second. As it stands now when I debug the program and look at my "numbers[]" array I have 1 and then 11-28. How can I alter what I have in order to take every number in the array and put it into my dynamically allocated array? And I am at a total loss as to how to change the commas to tabs. Any help/advice is appreciated, thanks in advance.