473,465 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

newbie question reading in an array using a structure for the array type

5 New Member
so i'm trying to read in from a file but having lots of trouble so far all i get are segmentation faults or jibberish numbers when outputting and i just need a little bit of direction on how to solve this problem. i'm reading i don't have much experience with reading arrays in from a file and stuff. heres what i have.
Expand|Select|Wrap|Line Numbers
  1. struct date
  2. {
  3.       int day, month, year; 
  4. };
  5.  
  6. struct student
  7. {
  8.       string firstName, lastName;
  9.       int hours;
  10.       double points;
  11.       date dob;
  12. };
  13.  
those are my structs and my prime read is
Expand|Select|Wrap|Line Numbers
  1. students>>  listOfS[arraySize].lastName >> listOfS[arraySize].firstName >> listOfS[arraySize].hours >> listOfS[arraySize].points >> listOfS[arraySize].dob.day >> listOfS[arraySize].dob.month >> listOfS[arraySize].dob.year;
  2.  
  3. while(!students.eof())
  4.     { 
  5.                           arraySize++;
  6.                           cout <<  listOfS[arraySize].lastName << listOfS[arraySize].firstName << listOfS[arraySize].hours << listOfS[arraySize].points << listOfS[arraySize].dob.day << listOfS[arraySize].dob.month << listOfS[arraySize].dob.year;
  7.     }
  8.  
array size is initialized to 100 and i have error checking to make sure the file opens and the dat file is
Expand|Select|Wrap|Line Numbers
  1. Jones Pete 30 78.0 1 11 1973
  2. Brown Carl 64 198.4 6 7 1985
  3. Smith Ann 37 118.4 9 2 1980
  4. White Mary 54 143.1 3 17 1975
  5. Black John 47 87.4 12 10 1981
  6. Flyn Bee 58 145.0 8 15 1987
  7. Sikes Bob 23 88.78 2 11 1983
  8.  
thank you in advance
Dec 4 '07 #1
5 1599
oler1s
671 Recognized Expert Contributor
You honestly should read the forum rules about using CODE tags. It's more than rude to dump mass amounts of unformatted code. We can't read it properly, and we won't be able to help you.

Maybe the problem is somewhere else. But I already see a problem with:

Expand|Select|Wrap|Line Numbers
  1. while(!students.eof())
  2. {
  3.     arraySize++;
  4.  
eof() does not behave like you imagine it does. That is why you should always read the documentation before you use it. The documentation will state something like it returns true if the eof flag on a stream is set. I'm going to stop here, and see if you intuitively see the problem. If not, I'll explain further.
Dec 4 '07 #2
traviswhiskey
5 New Member
yeah i'm not understanding
Dec 4 '07 #3
traviswhiskey
5 New Member
so whats wrong with the eof()?
Dec 4 '07 #4
traviswhiskey
5 New Member
can anyone else help me out?
Dec 4 '07 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
Jones Pete 30 78.0 1 11 1973
With data line this if you use the >> operator into a string all you get is Jones. The >> operator skips whitespace.

You can only use the >> operator when you know the format of the data and your data has unknown format. I say this because this data:
Expand|Select|Wrap|Line Numbers
  1. weaknessforcats 30 78.0 1 11 1973
  2.  
looks the same but has fewer data elements.

You need to a) alter your input by inerting field separators so you know where your data elements are, or b) read the entire line in as a single string and then parse out the elements from the string.
Dec 5 '07 #6

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

Similar topics

7
by: Santah | last post by:
hi I'm new to C++ and I'm currently working on Visual C++ 6.0 I'm trying to open a text file, and read some data from it part of the text file looks like this: --------
1
by: Jonathan Amend | last post by:
I'm trying to port some C++ code to VB.NET but I have hit a snag. I need to add the pointer of a structure which includes an array to an array and then pass it to an API. Here are the two code...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
8
by: Sam | last post by:
I have a situation occuring in my code and I just can't see to figure out why I have an structure called employee that will put all of the employee id's into a char array set to 10 struct...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
3
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I'm trying to write an array of structures named myStructArray to a binary file and later on read it back. Although I could complete the entire project in C in about 2 minutes, I...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.