Access Write Location Violation ERROR??

I am getting a write violation error on this code snippet. If I remove this snippet of code the program works fine but while it is there I get the run time ERROR. I don't understand why because that code isn't even writing anything. It's just reading it out to the screen. What is causing this ERROR and how might I go about fixing it?

1
2
3
4
for (int i = 0; i < 100; i++)	//                <-------  This is giving 
 {  //                                            <-------  me a ACCESS 
	cout << bookArray[i].market << endl;//    <-------- WRITE LOCATION  
 }  //                                            <-------  VIOLATION  


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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

#define MAX_BOOKS 500

 struct bookType
	{
	public:
		string item_name;
		string listing_id;
		string seller_sku;
		string price;
		string quantity;
		string open_date;
		string item_note;
		string item_condition;
		string product_id;
		string market;
		string book_header_line;
	 };

string headings[10];
//======================================================================
int main()
{
 ifstream infile;
 ofstream outfile;

 bookType MyBooks;
 bookType bookArray[MAX_BOOKS];
 void getHeadings(ofstream& outfile,istream& infile, string headings[10]);
 void ReadBooks(ofstream& outfile,ifstream &infile, bookType bookArray[MAX_BOOKS]);

 outfile.open ("report.txt");  
 infile.open("books.txt");
 if(!infile)
  {
  cout << "Unable to open input book file!" << endl ;
  system ("pause");
  return 1;
  }

 cout << "Processing Data" << endl;

 getHeadings(outfile , infile, &headings[10]);

 ReadBooks(outfile, infile, &bookArray[MAX_BOOKS]);

for (int i = 0; i < 100; i++)	//<--------------------------------  This is giving 
 {  //<------------------------------------------------------------  me a ACCESS 
	cout << bookArray[i].market << endl;//<------------------------  WRITE LOCATION  
 }  //<------------------------------------------------------------  VIOLATION ERROR.

 system("pause");
 return 0;
}
//=======================================================================
 void getHeadings(ofstream& outfile,istream& infile,string headings[10])
 {
	for (int i = 0; i < 10; i++)
	{
		infile >> headings[i];
		cout << headings[i] << endl;
	}
outfile << headings[0]
		<< setw(headings[3].size() + 60)<< headings[3]
		<< setw(headings[4].size() + 7) << headings[4] 
		<< setw(headings[8].size() + 4) << headings[8] 
		<< setw(headings[8].size() + 3) << headings[9] <<endl;
		for(int i = 0; i < 175; i++)
		{                    
			outfile << "_";
		}
		outfile << endl;
 }

 void ReadBooks(ofstream &outfile,ifstream &infile, bookType bookArray[MAX_BOOKS])
 {
 int i = 0;
  while(!infile.eof() && i < MAX_BOOKS)
 {
        getline(infile,bookArray[i].item_name, '\t');
        getline(infile,bookArray[i].listing_id, '\t');
        getline(infile,bookArray[i].seller_sku, '\t');
        getline(infile,bookArray[i].price, '\t');
        getline(infile,bookArray[i].quantity, '\t');
        getline(infile,bookArray[i].open_date, '\t');
        getline(infile,bookArray[i].item_note, '\t');
        getline(infile,bookArray[i].item_condition, '\t');
        getline(infile,bookArray[i].product_id, '\t');
        getline(infile,bookArray[i].market, '\n');
        //tPrice = atof(MyBooks.price.c_str());
        outfile << bookArray[i].item_name << setw(100 -       bookArray[i].item_name.size()) << '\t'
				<< bookArray[i].price << setw(10 - bookArray[i].price.size()) << '\t'    
				<< bookArray[i].quantity << '\t' 
				<< bookArray[i].product_id << '\t' 
				<< bookArray[i].market << endl;


        i++;

 }
 }
Last edited on
I think the problem is in this call

getHeadings(outfile , infile, &headings[10]);

You are passing to the function an address that is beyond the array headings.
Last edited on
I'm not sure I follow you. The headings array holds 10 values. At least its declared that way on line 25. What an I missing? Also if it were the headings call then when I remove the code that I have flagged as troublesome, I would still get the error but I don't. After the following code is removed it runs fine.

1
2
3
4
for (int i = 0; i < 100; i++)	//<--------------------------------  This is giving 
 {  //<------------------------------------------------------------  me a ACCESS 
	cout << bookArray[i].market << endl;//<------------------------  WRITE LOCATION  
 }  //<------------------------------------------------------------  VIOLATION ERROR. 


I even tried replacing the trouble code with just this simple cout statment and still got the error.

1
2
3

	cout << bookArray[4].market << endl;

Last edited on
OK... Scratch this entire post... Sorry to waste your time. There is an issue with my compiler that is causing the problem so none of these questions are valid.

Sorry

Sorry

Sorry
1. As vlad pointed out headings[10] is out of range since the indexes for headings run from 0-9.

2. Your two function declarations on lines 34 and 35 cannot go inside main(). Move then before main() right after the line 25 string headings[10];

3. Inside of void getHeadings(ofstream& outfile,istream& infile,string headings[10])
since you are subscripting headings to access the strings in the array you need a pointer to a string and not a string itself as the parameter.. The name headings by itself (without the [] brackets) is a pointer to the first element of the array. The way you are trying to pass the array to your function will not work. Change your function declaration and definition to:

void getHeadings(ofstream& outfile,istream& infile,string* pString )

Now you can call the function as:

getHeadings(outfile , infile, headings );

or as

getHeadings(outfile , infile, &headings[0]);

These two calls are equivalent, both passing a pointer to the first element of the headings array.

4. You may have a similar problem in
void ReadBooks(ofstream &outfile,ifstream &infile, bookType bookArray[MAX_BOOKS])

I haven't looked any further.
Last edited on
To make things a little bit simpler I have removed all reference to headings and heading arrays. the issue I am having is within the readBooks() function.
I am getting a write violation error somewhere in this 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
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

#define MAX_BOOKS 500

 struct bookType
	{
	public:
		string item_name;
		string listing_id;
		string seller_sku;
		string price;
		string quantity;
		string open_date;
		string item_note;
		string item_condition;
		string product_id;
		string market;
		string book_header_line;
	 };

string headings[10];

 void ReadBooks(ofstream& outfile,ifstream &infile, bookType bookArray[MAX_BOOKS]);
//======================================================================
int main()
{
 ifstream infile;
 ofstream outfile;

 bookType MyBooks;
 bookType bookArray[MAX_BOOKS];

 outfile.open ("report.txt");  
 infile.open("books.txt");
 if(!infile)
  {
  cout << "Unable to open input book file!" << endl ;
  system ("pause");
  return 1;
  }

 cout << "Processing Data" << endl;

 ReadBooks(outfile, infile, &bookArray[MAX_BOOKS]);

 system("pause");
 return 0;
}
//=======================================================================

 void ReadBooks(ofstream &outfile,ifstream &infile, bookType bookArray[MAX_BOOKS])
 {
 int i = 0;
  while(!infile.eof() && i < MAX_BOOKS)
 {
        getline(infile,bookArray[i].item_name, '\t');
        getline(infile,bookArray[i].listing_id, '\t');
        getline(infile,bookArray[i].seller_sku, '\t');
        getline(infile,bookArray[i].price, '\t');
        getline(infile,bookArray[i].quantity, '\t');
        getline(infile,bookArray[i].open_date, '\t');
        getline(infile,bookArray[i].item_note, '\t');
        getline(infile,bookArray[i].item_condition, '\t');
        getline(infile,bookArray[i].product_id, '\t');
        getline(infile,bookArray[i].market, '\n');
        outfile << bookArray[i].item_name << setw(100 -       bookArray[i].item_name.size()) << '\t'
				<< bookArray[i].price << setw(10 - bookArray[i].price.size()) << '\t'    
				<< bookArray[i].quantity << '\t' 
				<< bookArray[i].product_id << '\t' 
				<< bookArray[i].market << endl;


        i++;

 }
 }
The issue is the same as it was with your previous code.

bookArray[MAX_BOOKS] is one past the end of the valid elements of bookArray. Don't access it.

Change line 48 to ReadBooks(outfile, infile, bookArray) ;
Last edited on
There it is.. Ok thank you all for your help. I am very grateful for all the feedback I got. Although I was not forced to change the header array code it did need the readBook code changed. The working code is here. Its not really working but it is compiling and running now. I put some comment notes in the code where the strange occurrences are happening. I am really stumped about the MAX_BOOKS int not allowing me to set it lower than 201. If I am only reading 100 bookTypes into the array shouldnt I be able to set MAX_BOOKS to 101?

EDIT: I forgot to change line 48 as instructed above. When I did everything worked just like it was supposed to.. Special thanks to you cire :}

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

const int MAX_BOOKS = 201; //<--------------If i set this to anything less than 200 I get the
		          //<------------- WRITE LOCATION VIOLATION runtime ERROR
 struct bookType
	{
	public:
		string item_name;
		string listing_id;
		string seller_sku;
		string price;
		string quantity;
		string open_date;
		string item_note;
		string item_condition;
		string product_id;
		string market;
		string book_header_line;
	 };

string headings[10];
//======================================================================
int main()
{
 ifstream infile;
 ofstream outfile;

 bookType MyBooks;
 bookType bookArray[MAX_BOOKS];
 void getHeadings(ofstream& outfile,istream& infile, string headings[10]);
 void ReadBooks(ofstream& outfile,ifstream &infile, bookType bookArray[MAX_BOOKS]);

 outfile.open ("report.txt");  
 infile.open("books.txt");
 if(!infile)
  {
  cout << "Unable to open input book file!" << endl ;
  system ("pause");
  return 1;
  }

 cout << "Processing Data" << endl;

 getHeadings(outfile , infile, &headings[10]);

 ReadBooks(outfile, infile, &bookArray[100]);

for (int i = 0; i < 100; i++)	//<--------------------------------  This is printing 
 {  //<------------------------------------------------------------  lots of empty
	cout << bookArray[i].market << endl;//<------------------------  space to the  
 }  //<------------------------------------------------------------  screen.

 system("pause");
 return 0;
}
//=======================================================================
 void getHeadings(ofstream& outfile,istream& infile,string headings[10])
 {
	for (int i = 0; i < 10; i++)
	{
		infile >> headings[i];
		cout << headings[i] << endl;
	}
outfile << headings[0]
		<< setw(headings[3].size() + 60)<< headings[3]
		<< setw(headings[4].size() + 7) << headings[4] 
		<< setw(headings[8].size() + 4) << headings[8] 
		<< setw(headings[8].size() + 3) << headings[9] <<endl;
		for(int i = 0; i < 175; i++)
		{                    
			outfile << "_";
		}
		outfile << endl;
 }

 void ReadBooks(ofstream &outfile,ifstream &infile, bookType bookArray[])
 {
 int i = 0;
  while(!infile.eof() && i < MAX_BOOKS)
 {
        getline(infile,bookArray[i].item_name, '\t');
        getline(infile,bookArray[i].listing_id, '\t');
        getline(infile,bookArray[i].seller_sku, '\t');
        getline(infile,bookArray[i].price, '\t');
        getline(infile,bookArray[i].quantity, '\t');
        getline(infile,bookArray[i].open_date, '\t');
        getline(infile,bookArray[i].item_note, '\t');
        getline(infile,bookArray[i].item_condition, '\t');
        getline(infile,bookArray[i].product_id, '\t');
        getline(infile,bookArray[i].market, '\n');
        outfile << bookArray[i].item_name << setw(100 -       bookArray[i].item_name.size()) << '\t'
				<< bookArray[i].price << setw(10 - bookArray[i].price.size()) << '\t'    
				<< bookArray[i].quantity << '\t' 
				<< bookArray[i].product_id << '\t' 
				<< bookArray[i].market << endl;


        i++;

 }
 }

Last edited on
Topic archived. No new replies allowed.