File opening method problem (division by zero)

Hello there ^^

While opening files and reading them, I've come across two different methods, they both work, but not when I try combining the two methods.

1
2
3
/* === declared beforehand === */
typedef vector<string> row;
typedef vector<row> matrix;

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
/* === function === */
// variables.
char c;
int row = 0;
int cell = 0;
static const char* filename = "example.csv"; //this is placed in the main, but it's fine for now

// open file first time
ifstream file1;
file1.open(filename);
while (file1.good())
{
	c = file1.get();
	if (c == ',') 
	{
		cell++;
	}
	if (c == '\n') 
	{
		row++;
	}
}
file1.close();

int column = cell / row; //now it works fine

//open same file other time >_<"
ifstream file2 (filename);
if (file2.is_open())
{
	//ofstream coutput("debug.txt");
	//get string info 
	for (int i=0;i<table.size();i++)
	{
		for (int j=0;j<table[i].size(); j++)
		{
			getline(file2, table[i][j], ',');
			cout << table[i][j] << ", " ;
			//coutput << table[i][j] << ", " ;
		}
	}
	cout << endl << endl << "DONE" << endl << endl; 
}else cout << "Error opening the file(2)" << endl;
file2.close();


I left out a lot of the code since it's really long, everything works fine as it is right now, I'm just wondering why I can't convert the first file reading part to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ifstream file1 (filename);
if (file1.is_open())
{
	c = file1.get();
	if (c == ',') 
	{
		cell++;
	}
	if (c == '\n') 
	{
		row++;
	}
}else cout << "Error opening the file(1)" << endl;
file1.close();


While debugging the program I get this error:
Unhandled exception at 0x00932b71 in ilikecoffee.exe: 0xC0000094: Integer division by zero.

When breaking: cell has a value of 1, column has a value of -858993460.

somehow the column and cell size don't get updated properly with method 2. :s


Thanks in advance!

~ Rii

----------

complete code below (incase there's something going wrong somewhere else):
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

typedef vector<string> row;
typedef vector<row> matrix;

bool exit();
void setMatrix(string filename, matrix *data);

int main()
{
	static const char* filename = "example.csv";
	ifstream file(filename);
	matrix pathway;

	if (file.is_open())
	{
		setMatrix(filename, &pathway);

		// quick matrix size set success check.
		cout << endl << "Table row size is: " << pathway.size() << endl;
		for (int i = 0; i < pathway.size(); i += pathway.size())
			cout << "Table column size is: " << pathway[i].size() << endl << endl; 

		//print string
		for (int i=0;i<pathway.size(); i++)
		{
			for (int j=0;j<pathway[i].size(); j++)
			{
				cout << pathway[i][j] << ", ";
			}
			//cout << endl << endl;
		}
	} else cout << "Error opening the file" << endl;
	file.close();

	exit();
}

bool exit()
{
	cout << endl << endl << "Press ENTER to continue...";
	cin.ignore(numeric_limits<streamsize>::max(), '\n' );
	return 0;
}

void setMatrix(string filename, matrix *data)
{
	// variables.
	char c;
	int row = 0;
	int cell = 0;
	matrix table;

	// get rows/columns from file.
	ifstream file1;
	file1.open(filename);
	while (file1.good())
	{
		c = file1.get();
		if (c == ',') 
		{
			cell++;
		}
		if (c == '\n') 
		{
			row++;
		}
	}
	file1.close();

	//quick debug.
	int column = cell / row;
	cout << "Rows = " << row << endl << "Columns = " << column << endl << "Cells = " << cell << endl << endl;
	
	// resize matrix.
	table.resize(row);
	for (int i = 0; i < row; i++)
	{
		table[i].resize(column);
	}

	//open file again >_<"
	ifstream file2 (filename);
	if (file2.is_open())
	{
		ofstream coutput("debug.txt");
		//get string info 
		for (int i=0;i<table.size();i++)
		{
			for (int j=0;j<table[i].size(); j++)
			{
				getline(file2, table[i][j], ',');
				cout << table[i][j] << ", " ;
				coutput << table[i][j] << ", " ;
			}
		}
		cout << endl << endl << "DONE" << endl << endl; 
	}else cout << "Error opening the file(2)" << endl;
	file2.close();

	// pass values by pointer.
	*data = table;
}


part of example.csv
1
2
3
4
Aldosterone,361.20095,361.19693,-11.13,6.8,6.812,1,242,0,0,0,0,0,242
Cortisol,363.2166,363.21543,-3.21,7.71,7.73,57,441865,8073,438,534,582,0,87870
Cortisone,361.20095,361.20047,-1.33,7.72,7.72,47,28303,1190,578,478,106,345,0
T-gluc-D3,468.26712,468.26571,-3,8.47,8.488,63,364155,5903,7833,6591,6646,5539,6052
Last edited on
It's running fine for me here.

What line is the crash happening on?
In the second version you are not using a loop so you will only handle the first character in the file which is 'A' so row and cell is still zero when you use them in division.

Shouldn't row be initialized to 1 because there is always one row?

Last edited on
if file1 cannot be opened row remains 0 and your program will crash
It's running fine for me here.

What line is the crash happening on?

The full code is working, but in the middle of my post I said something about another version of opening a file, that doesn't work somehow, it compiles fine, but while running it crashes.

In the second version you are not using a loop so you will only handle the first character in the file which is 'A' so row and cell is still zero when you use them in division.

Shouldn't row be initialized to 1 because there is always one row?

ooh I see, thank you!

and yes, it should be 1! my bad, I just had a little discussion with my supervisor(was proud of a working program so I showed him) and he said the same, the amount of rows/columns are correct now.

if file1 cannot be opened row remains 0 and your program will crash

I set row to 1 now because of the mistake with counting. thanks for explaining!

To all: thanks for your answers!
I managed to fix it. ^^

~ Rii
Last edited on
Topic archived. No new replies allowed.