Reason for unhandled exception for given code

Hello,

Background:

I have written a piece of code for reading numbers (line by line) in a file and looping over n number of files in my dir. With each txt file being read, I am dumping data in console and also creating a numbered csv file.

Meaning corresponding to data_0.txt >>> file_0.csv
data_1.txt >>> file_1.csv
and so on.

The maximum number of txt files to be read should be known and also the format of file name.

I have put comments in the file(hopefully enough to self explain what I am doing).Apologies if they are not in line with standards. I am a beginner.

Problem:

The problem is when k = 0 in my for loop, everything works fine.

>> I see the array in console corresponding to first file
>> Also, file_0.csv gets generated with the right data.

but I believe that for k = 1, the program fails and I get the following error:

Unhandled exception at 0*00411b00 in sample1.exe. Access violation writing location 0*ceb2eb48.


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
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <iomanip> //in case setting width. use later on
#include <windows.h>
#include <cstdlib>

//Adding functionality for accessing the csv files from matlab for computation
//Have excluded that part for brevity 
/*#include <engine.h> // matlab engine


#pragma comment(lib, "libmx.lib")
#pragma comment(lib, "libmex.lib")
#pragma comment(lib, "libeng.lib")
#pragma comment (lib, "libmat.lib")	*/

using namespace std;

//defining a function to append numbering to filename
//say data_0.txt, data_1.txt... when reading file
// and file_0.csv, file_1.csv ... when writing out data in loop. 
string Inttostr(int n)
{
 stringstream result;
 result << n;
 return result.str();
}


int main()
{

	int rowA = 0;
	int colA = 0;

	double x;
	string line;
	string text;
	string filename;
	int filenumber;
	double arrayA[100][10] = {};   // define a tentative array size. maximum that you may expect for known data. 

	//Asking user for the maximum files in the given dir. that he will read from:
	
	cout << "Enter the number of files to be read:";
	cin >> filenumber;

	//this for loop run to end of this program
	//basically, looping over the files one by one and in it the while loops are reading data per file and 
	//writing out the same 
	for (int k = 0; k < filenumber; k++)

	{
		ifstream infile;
		filename = "data_" + Inttostr(k) + ".txt"; // user needs to have files in data_0.txt, data_1.txt.. so on format. 
		infile.open(filename.c_str());

		//Error check
		if (infile.fail())
			{
			cerr << "Error reading the file:" << endl;
			exit(1);
			}
		
			//Reading data from file
			while(getline(infile,line))
			{
						istringstream streamA(line);
							colA = 0;
						while (streamA >> x)
							{
								arrayA[rowA][colA]= x;
								colA++;
							}
						rowA++;
			}
				cout << "No. of rows:" << rowA << endl;
				cout << "No. of cols:" << colA << endl;
				cout << '\n';
	

			//Display array in the console
			for (int i = 0; i < rowA; i++)
				{
					for (int j = 0; j<colA; j++)
						{
							cout << setprecision(10);
							cout << arrayA[i][j]<< " ";
						}
							cout << endl;
				}
		Sleep(10000);	  // putting a pause here to check what the console looks like once k = 0
						//was expecting that after 20 secs (there is another 10 sec pause down the line) the console will update with k =1 in the same loop. 
			
		//write out the array in csv format
		text = "file_" +  Inttostr(k) + ".csv";
		ofstream fout(text.c_str(), ios::out);
		fout.precision(10);
			for (int i = 0; i< rowA; i++)
				{
				for (int j = 0; j<colA; j++)
					{
						fout << arrayA[i][j] << ',';
					}
				fout << endl;
				}

		Sleep(10000); // checking if file_0.csv is created and then the loop can run again to dump file_1.csv. 

	} // the outmost for loop ends here. 	
	
	
	return 0;

}


Comment: Can you please point me to right direction. Also, I would appreciate if you can hint at a better way to do this process so I can write a better code.

Thanks & Regards.
An access violation is often caused by dereferencing an invalid pointer, accessing arrays out of bounds and things like that. Shouldn't you reset the value of rowA when you start reading from a new file?
Oh my goodness....how stupid of me.....

int rowA = 0

and int colA = 0 should be inside for loop.......


It works now........I just slapped myself.

Thank you sir! I am from mechanical engg background, but even a beginner like me should not commit such foolish mistake.

sorry for wasting your time.
Topic archived. No new replies allowed.