Program received signal: “EXEC_BAD_ACCESS”.

Hello Everyone,

I am coding a class ArrowsCounting which implements a method
LineAccounterT(const char * archivos2[]). It should return the Number of arrows in a file.

It work fine if the Size of archivos2 <18 but for SizeArchiv2>18

the Program received signal " “EXEC_BAD_ACCESS" segmengation fault.

Do you Known what is wrong with this program ?

Thanks

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

//
int ArrowsCounting::LineAccounterT( const char* rawFiles [] )
{
    count2=0; //! it was declared in the  ArrowsCounting.hh
	string line;
	int SizeArchiv2=sizeof(rawFiles)/sizeof(const char*);
	const char *Title="Date Open High Low Close Volume Adj Close";
	
	ifstream myfiles[SizeArchiv2];
	for (int k=0; k<SizeArchiv2; k++)
	{ //cout<<"This file "<<rawFiles[k]<<endl;
		myfiles[k].open(rawFiles[k]);
		if(myfiles[k].is_open() ) 
		{
			while (!myfiles[k].eof() )
			{
				getline(myfiles[k], line);
				if(line!=Title)
				{
					++count2;
				}
				
				
			}//!While
			
			
		}

		else cout << "Unable to open file\n"<<k; 
		myfiles[k].close();
	} 
		
	return count2-3;

};



then i implement this Class in main so
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
 

class ArrowsCounting flecha[SizeArchiv2];

int SizeArchiv2;

//! Files to be processed
archivos2[]={"/Users/mprez/Desktop/kit_programm/AAPL_1996bis2009.txt ",
		" /Users/mprez/Desktop/kit_programm/AA_1996bis2009.txt ",
		" /Users/mprez/Desktop/kit_programm/ABC_1996bis2009.txt ",
		" /Users/mprez/Desktop/kit_programm/ABT_1996bis2009.txt ",
		" /Users/mprez/Desktop/kit_programm/ACE1996bis2009.txt ",
		" /Users/mprez/Desktop/kit_programm/ACINF_1996bis2009.txt ",
		" /Users/mprez/Desktop/kit_programm/ADBE_1996bis2009.txt ",
		" /Users/mprez/Desktop/kit_programm/ADI_1996bis2009.txt ",
		" /Users/mprez/Desktop/kit_programm/ADM_1996bis2009.txt ",
		" /Users/mprez/Desktop/kit_programm/ADP_1996bis2009.txt ",
		" /Users/mprez/Desktop/kit_programm/ADSK_1996bis2009.txt ",
		" /Users/mprez/Desktop/kit_programm/AEE_1996bis2009.txt ",
		" /Users/mprez/Desktop/kit_programm/AEP_1996bis2009.txt ",
                " /Users/mprez/Desktop/kit_programm/AES_1996bis2009.txt ",
		" /Users/mprez/Desktop/kit_programm/AET_1996bis2009.txt ",
}

//! It calculates the Number of Files to be edited 

SizeArchiv2=sizeof(archivos2)/sizeof( const char*);

//!Because i need the Number of lines for a serie of files, i did define the
 

int  Js_Size[SizeArchiv2];// such as an Array of Number of Lines in the Files "archivos"

//!######################################
	//!--------->Lines in Files<-------------
	//!######################################
	for (int k=0; k<SizeArchiv2;k++)
	{
	 Js_Size[k]=flecha[k].LineAccounterT(archivos2);
}
	
	


There are many things wrong with that program.
First, rawFiles is a pointer to a pointer to const char. SizeArchiv2 will always be 1. Pass a string vector instead.
Second, the size of an array must be a constant known at compile-time. You cannot use SizeArchiv2 as the size. Again, use vector. Or in this particular case, get rid of myfiles entirely - there's no point in keeping the old fstream objects.
Third, your condition in the while loop will cause problems on some implementations. You should write while (!getline(...)) instead.
Edit: and fourth, you cannot compare char pointers (well, you can, but it certainly won't do what you want to). Use string.
Last edited on
Thank you for your help, but it returns only the total line of the all files
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

int ArrowsCounting::LineAccounter2( const vector <string> & rawFiles )
{count2=0;
	string line;
	
	const char* Title="Date Open High Low Close Volume Adj Close";
	
	int  rf_size=rawFiles.size();
	ifstream myfiles[rf_size]; 
	const char* p[rf_size];
	
	for (int k=0; k<rf_size; k++)	
	{
		p[k]=rawFiles[k].c_str();
		cout<<"P[k]"<<p[k]<<endl;
	//myFiles[k].open(rawFiles[k]);
		myfiles[k].open(p[k]);
		
		if(myfiles[k].is_open() ) 
		{
			while (!myfiles[k].eof() )
			{
				getline(myfiles[k], line);
					cout<<line<<endl;
			if(line!=Title)
				{
					cout<<count2<<endl;
					count2++;
				}
				
				
				
			}//!While
		}
		//cout<<"rawFiles"<< rawFiles[k]<<endl;
		else cout << "Unable to open file\n"<<k; 
	//	myfiles[k].close();
		
	}		
	return count2-3;
}

With
1
2
 
int ArrowsCounting::LineAccounterT( const char* rawFiles [] )

return up to0 16 differents size of files and i need 480

and
1
2
3
4
 

int ArrowsCounting::LineAccounter2( const vector <string> & rawFiles )

returns the sum of total maxima lines of rawFiles

my question is
how can i do to return LineAcccounter like a array of sizes?
Thanks
Hello everyone, i got it

i coded my LineAccounter like a vector function and
it works

thank you
mprez
Topic archived. No new replies allowed.