Reading Data from file into 2-d array trouble

Hi all,

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <sstream>

using namespace std;
int linecount(string);
string fileName;
const int 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.


0:00:00	8701	8846	9029	8434	8912	8579	8938	8493
0:00:06	8604	8786	8966	8446	8886	8541	8899	8492
0:00:12	8582	8764	8973	8430	8831	8532	8905	8459
0:00:18	8563	8743	8983	8403	8819	8523	8891	8438
0:00:24	8553	8686	8978	8371	8836	8492	8857	8413
0:00:30	8542	8645	8939	8369	8818	8482	8838	8368
0:00:36	8510	8628	8934	8340	8797	8481	8809	8343
0:00:42	8479	8577	8909	8333	8762	8445	8761	8349
0:00:48	8431	8534	8887	8307	8715	8459	8740	8322
0:00:54	8395	8485	8891	8306	8725	8416	8681	8296
0:01:00	8388	8454	8861	8285	8705	8411	8633	8261
0:01:06	8359	8430	8865	8263	8679	8396	8613	8249
0:01:12	8349	8380	8823	8241	8636	8365	8595	8244
0:01:18	8319	8347	8836	8223	8610	8382	8561	8216
0:01:24	8285	8331	8809	8211	8601	8346	8557	8188
0:01:30	8274	8306	8797	8208	8579	8344	8519	8181
0:01:36	8229	8283	8773	8190	8579	8313	8493	8127
0:01:42	8205	8268	8759	8160	8527	8312	8452	8143
0:01:48	8212	8251	8745	8184	8514	8303	8449	8097
0:01:54	8183	8211	8719	8140	8491	8295	8413	8100
Last edited on
First read this:

http://en.wikipedia.org/wiki/Segmentation_fault

Second you must open the file before you can read from it. check out the ifstream API:

http://www.cplusplus.com/reference/iostream/ifstream/
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?

I wrote a line to open the file (line 36).


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <sstream>

using namespace std;
int linecount(string);
string fileName;
const int 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;
}
Topic archived. No new replies allowed.