I'm trying to write a program that will take tab delimited data from a text file and put it into a 2 dimensional array. I've been doing some research on a few forums on how to do this and the one that appeared the most clear to me (a very non-experienced C++ programmer) was to use a stringstream and move the data into a temporary array before putting it into the final one.
When I run the program it crashes. I'm using the Code::Blocks environment, and I get an error "Process terminated with status -1073741571". I've also used the debugger and I get a "Program received signal SIGSEGV, Segmentation fault" message. I have no idea what this means so if someone would let me know that would be great. In general I really don't know what's wrong with the code.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <sstream>
usingnamespace std;
int linecount(string);
string fileName;
constint BUFFSIZE = 10240;
int linecount(string someFile)
{
int number_of_lines;
string line;
ifstream myFile(someFile.c_str());
while(getline(myFile, line))
++number_of_lines;
return number_of_lines;
}
int main()
{
int nNumWells;
cout << "Enter the name of the text file: ";
cin >> fileName;
cout << "Enter the number of wells (columns of data): ";
cin >> nNumWells;
int rows = linecount(fileName);
int cols = nNumWells;
char buffarray[BUFFSIZE];
float dataArray[rows][cols + 1];
ifstream inFile(fileName.c_str());
stringstream ss;
if(!inFile)
{
cout << "Error! Couldn't read file." << endl;
system("pause");
return -1;
}
for(int row = 0; row < rows; row++) //read a full line into the buffer array
{
inFile.getline(buffarray);
ss << buffarray; //put it into the string stream
for( int col = 0; col < cols + 1;col++)
{
ss.getline(buffarray, 15, '\t'); //this now takes the tab delimited values and puts them back into the buffer array.
dataArray[row][col] = atof(buffarray); //this puts the data into the 2-d array.
}
ss << ""; //this clears the stringstream
ss.clear();// this clears the 'end of file' flag.
}
//Check the array by printing the contents
for(int row = 0; row<rows; row++)
{
for(int col = 0; col<cols;col++)
{
cout << dataArray[row][col]<<" ";
}
cout << endl;
}
inFile.close();
system("pause");
return 0;
}
Also, here's an example of what the input text file looks like.
OK, so I now understand that the segmentation error was the result of addressing some non-existent memory location, and I'm guessing that it's because I'm overwriting the array buffarray? I'm still not sure what exactly is causing this error. How would I fix this?
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <sstream>
usingnamespace std;
int linecount(string);
string fileName;
constint BUFFSIZE = 10240;
int linecount(string someFile)
{
int number_of_lines;
string line;
ifstream myFile(someFile.c_str());
while(getline(myFile, line))
++number_of_lines;
return number_of_lines;
}
int main()
{
int nNumWells;
cout << "Enter the name of the text file: ";
cin >> fileName;
cout << "Enter the number of wells (columns of data): ";
cin >> nNumWells;
int rows = linecount(fileName);
int cols = nNumWells + 1;
char buffarray[BUFFSIZE];
float dataArray[rows][cols];
stringstream ss;
ifstream inFile;
inFile.open(fileName.c_str(), ifstream::in);
if(!inFile)
{
cout << "Error! Couldn't read file." << endl;
system("pause");
return -1;
}
for(int row = 0; row < rows; row++) //read a full line into the buffer array
{
inFile.getline(buffarray, BUFFSIZE);
ss << buffarray; //put it into the string stream
for( int col = 0; col < cols;col++)
{
ss.getline(buffarray, 15, '\t'); //this now takes the tab delimited values and puts them back into the buffer array.
dataArray[row][col] = atof(buffarray); //this puts the data into the 2-d array.
}
ss << ""; //this clears the stringstream
ss.clear();// this clears the 'end of file' flag.
}
//Check the array by printing the contents
for(int row = 0; row<rows; row++)
{
for(int col = 0; col<cols;col++)
{
cout << dataArray[row][col]<<" ";
}
cout << endl;
}
inFile.close();
system("pause");
return 0;
}