471,895 Members | 2,330 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,895 software developers and data experts.

Trying to insert into vector<struct_type> results in "Access Violation" Error...


I want to read data from a txt-file that looks like this:

aaa 4 12.45
bbb 3 7.34
ccc 12 3.45

and store the data in an array of structures. The struct and array are defined like this

Expand|Select|Wrap|Line Numbers
  1. struct proPkaData {
  2.         char Name[4];
  3.         int Index;
  4.         float Pka;
  5.         } Data;
  7. vector<proPkaData> DataArray;
The main program looks something like this:

int i=0;
while (!infile.eof()){

The program compiles and run but immediately gives an error: "Access Violation at address 00401789". I can't figure out the reason for that?

Thanks for the help!
Oct 5 '06 #1
2 2775
9,065 Expert Mod 8TB
int i=0;
while (!infile.eof()){
This is because DataArray[i] access data in the vector that already exists but your vector is probably empty.

either set the vector size before you fill it using setsize (???) or write your data like this

Expand|Select|Wrap|Line Numbers
  1. int i=0;
  2. proPkaData data;
  3. while (!infile.eof()){
  4.     infile>>data.Name;
  5.     DataArray.push_back(data);
  6.     i++;
Oct 5 '06 #2
THanks a lot, that worked out fine.

Another problem though is that the last line of the txt-file is being read twice. This seems strange since I'm using

while(!infile.eof() )

as the condition for the loop. Is that something that comes "as a bonus" when using ifstream or why is the line being read twice?
Oct 5 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Bercin Ates via SQLMonster.com | last post: by
2 posts views Thread by jw56578 | last post: by
1 post views Thread by OGG | last post: by
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.