473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

13
Hi,

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;
  6.  
  7. vector<proPkaData> DataArray;
The main program looks something like this:

int i=0;
while (!infile.eof()){
infile>>DataArray[i].Name;
i++

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 2864
Banfa
9,065 Expert Mod 8TB
int i=0;
while (!infile.eof()){
infile>>DataArray[i].Name;
i++.
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++;
  7.  
Oct 5 '06 #2
Curten
13
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

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

Similar topics

3
by: JC-Atl | last post by:
I am trying to do this in the SQL page of phpMyAdmin, but I get a syntax error. Can someone please help me out ? Thanks for your time and efforts! JC INSERT INTO `Test` ( `pkey` , `col2` , ...
1
by: Cliff | last post by:
I'm trying to do multiple insert statements. The table looks like this: CREATE TABLE $table (CNTY_CNTRY_CD char(3),ST char(2), CNTY_CNTRY_DESCR varchar(50),CNTY_CNTRY_IND char(1),...
7
by: Bercin Ates via SQLMonster.com | last post by:
I?m getting an error when I execute a stored procedure which is try to insert a row to a table. The error is: Server: Msg 1934, Level 16, State 1, Procedure SRV_SP_IS_EMRI_SATIRI_EKLE, Line 32...
2
by: jw56578 | last post by:
Is it possible to take the result from an SP that returns 1 value and insert it into a variable?
3
by: Dmitry Jouravlev | last post by:
Hi, I have a number of C++ solutions in Visual Studio .NET and when i compile them using "Whole Program Optimization", certain projects report a LNK1171 error saying that c2.dll could not be...
1
by: sudhinma | last post by:
Hello all, I created a web form to enter a new record to a access database called student.mdb. But when i click the save button(button1) the record does not get added and i get a exception. the...
1
by: OGG | last post by:
Hi guys, I am trying to do a simple task: convert bitmap to raw and back to bitmap as fast as possible. Here are the 2 methods I am using: private byte ToRaw(Bitmap bmp) { BitmapData data =...
7
by: ShenPixel | last post by:
I've been working with the following code and would like to output the results into a customizable column variable, how would I go about doing such a thing, thanks in advance for any help. I hard...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.