Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 13th, 2004, 08:56 PM
Registered User
 
Join Date: Oct 2004
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
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
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
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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.