Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 13th, 2004, 08:56 PM
Registered User
 
Join Date: Oct 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default File to Structure?

How do i go from a file containing data, like ...

MJ914
Porsche
914-6
Irish_Green
2
2700
30000
40000

TO

struct car
{
char rego[6];
char make[15];
char model[10];
char colour[15];
int cap[4];
int doors[1];
int costp[6];
int sellp[6];
};

Reply With Quote
  #2 (permalink)  
Old October 13th, 2004, 09:44 PM
Registered User
 
Join Date: Oct 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

struct car
{
char rego[6];
char make[15];
char model[10];
char colour[15];
int cap[4];
int doors[1];
int costp[6];
int sellp[6];
};


void main()

ifstream fin;

for(x = 0; x++; x > 11 || x = EOF;)
    ifstream fin("cars.dat");
    fin >> car.rego >> car.make >> car.model >> car.colour >> car.cap >> car.doors >> car.costp >> car.sellp >>endl;

Reply With Quote
  #3 (permalink)  
Old October 13th, 2004, 10:02 PM
Registered User
 
Join Date: Oct 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I dont think thats the way to do it anyway ...

i think need to use getline();

Reply With Quote
  #4 (permalink)  
Old October 23rd, 2004, 05:29 AM
Authorized User
 
Join Date: Oct 2004
Location: San Francisco, CA, .
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You're not far off. The first issue I see is that your car struct is using arrays for cap, doors, costp, and sellp. Those fields are just numbers in your file, so they can just be ints. Here is the modified car structure:

struct car
{
  char rego[6];
  char make[15];
  char model[10];
  char colour[15];
  int cap;
  int doors;
  int costp;
  int sellp;
};


You had the right idea for reading in the data -- you can simply use >> on an input stream. You don't need to use endl at the end either. Here's a program that reads all the data in:

int main()
{
  struct car car;
  ifstream fin("cars.dat");
  fin >> car.rego >> car.make >> car.model >> car.colour >> car.cap >> car.doors >> car.costp >> car.sellp;
}



----
Scott J. Kleper
Author, "Professional C++"
(Wrox, 2005)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to view the remote file structure on ur machin Goldee J2EE 0 January 25th, 2006 03:57 AM
Database Structure aw23 VB Databases Basics 2 October 15th, 2005 01:20 PM
how to write a structure??? keepsmiling_sruti VB How-To 1 July 4th, 2005 11:09 PM
thePhile (VB) file folder structure... jaspe BOOK: ASP.NET Website Programming Problem-Design-Solution 1 September 1st, 2004 08:24 PM



All times are GMT -4. The time now is 12:19 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.