473,769 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

13 New Member
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>>DataArr ay[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 2900
Banfa
9,065 Recognized Expert Moderator Expert
int i=0;
while (!infile.eof()) {
infile>>DataArr ay[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 New Member
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.e of() )

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
2547
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` , `col3` ) VALUES ( SELECT max( `pkey` ) FROM Test, `rrrrrrrrr` , `wwwwwwwww` )
1
9888
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), HOME_CNTRY_IND char(1), CONSTRAINT cnty_key PRIMARY KEY (CNTY_CNTRY_CD, ST)) I'm using 2 fields for the primary key constraint My insert statement looks like this:
7
7297
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 INSERT failed because the following SET options have incorrect settings: 'ANSI_NULLS.'. In my sp, I insert an row to a table. But also I created a view which is select some fields from this table. (Note: Some fields are calculated fields in this...
2
2576
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
4144
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 loaded. The error contains the correct path to c2.dll (and it is definately there). This only happens on some projects and only when "whole program optimization" option is turned on. If i turn off this option, the problem goes away. I have other...
1
2816
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 code is in vb and i have used visual web developer 2005 express edition.the code is as under. IS this the right way to do it.If not pls suggest a better method. regards
1
1513
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 = bmp.LockBits(new Rectangle(new Point(0, 0), bmp.Size), ImageLockMode.ReadOnly, bmp.PixelFormat);
7
3880
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 coded the columns in the code below, but I would like the variable to be set so that a new row will start after a set number of columns. <? $allfiles = scandir('.'); $goodfiles = array(); foreach($allfiles as $f){ if(strpos($f,'.')!==0){...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.