473,320 Members | 1,829 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,320 software developers and data experts.

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];
};

Jul 22 '05 #1
7 1303
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;

Jul 22 '05 #2
I dont think thats the way to do it anyway ...

i think need to use getline();

Jul 22 '05 #3
On Wed, 13 Oct 2004 21:52:07 -0400, TheShadow1 wrote:
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];
These might be better off as std::string values, as defined in the
standard <string> header.
int cap[4]; // <--
int doors[1]; // <--
int costp[6]; // <--
int sellp[6]; // <--
};


The corresponding fields in your input data don't look like integer
*arrays* to me...

--
Some say the Wired doesn't have political borders like the real world,
but there are far too many nonsense-spouting anarchists or idiots who
think that pranks are a revolution.

Jul 22 '05 #4
TheShadow1 schrieb:
struct car
{
char rego[6];
char make[15];
char model[10];
char colour[15];
Those are dangerous and cannot be used for streaming as used below, use
std::string instead of char arrays.
int cap[4];
This declares an array of four integers where you presumably wanted an
integral type capable of storing values with up to four decimal digits.
int doors[1];
int costp[6];
int sellp[6];
Same for those.
};
void main()
main() always returns int (and its body needs enclosing braces like any
other function).

ifstream fin;

for(x = 0; x++; x > 11 || x = EOF;)
You never declared x nor EOF, there's one ; too much in the for
statement and the expressions' order is most probably wrong.
ifstream fin("cars.dat");
fin >> car.rego >> car.make >> car.model >> car.colour >> car.cap >>
car.doors >> car.costp >> car.sellp >>endl;


Except for missing error checking, that should work after dropping the
endl part and fixing the above issues.


HTH,
Regards,
Malte
Jul 22 '05 #5
So something more like ...

struct Car
{
string rego;
string make;
string model;
string colour;
int cap;
int doors;
int costp;
int sellp;
}

Jul 22 '05 #6

"TheShadow1" <tr********@gmail.com> wrote in message
news:1b******************************@localhost.ta lkaboutprogramming.com...
So something more like ...

struct Car
{
string rego;
string make;
string model;
string colour;
int cap;
int doors;
int costp;
int sellp;
}

Yes, looks a lot better.
Jul 22 '05 #7
TheShadow1 wrote:
So something more like ...

struct Car
{
string rego;
string make;
string model;
string colour;
int cap;
int doors;
int costp;
int sellp;
}


/* Data file from previous article:
MJ914
Porsche
914-6
Irish_Green
2
2700
30000
40000
*/

struct Car
{
// Same as above
friend istream& operator>>(istream& input, Car& car_);
};

istream& operator>>(istream& input, Car& new_car)
{
getline(input, rego);
getline(input, make);
getline(input, model);
getline(input, colour);
input >> cap >> doors >> costp >> sellp;
return input;
}

Now to use this:
int main(void)
{
ifstream data_file("data_file.txt");
Car my_car;

// Input car from data file:
data_file >> my_car;

return EXIT_SUCCESS;
}

I suggest you also overload operator<< so you can
display the contents of a Car.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Vasilis Serghi | last post by:
Hi all, I am fairly new to C programming and I have come to a stand still. I am trying to create a program that will accept a user input and give the user an output depending on the contents of a...
5
by: Chathu | last post by:
Hello everyone........... I have a problem on retriving a content of a binary file I wrote into. My program user structures, dynamic allocation of memory and files. I take the infomation into a...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
3
by: Zahid | last post by:
Hi, I have declared an array holding a custom declared structure. The structure looks like this: Private Structure MnuDataFrmFile Public menuGroup As String Public ItemDesc As String Public...
5
by: Phil Kelly | last post by:
Hi I need to write the contents of a structure to a binary file - there is one string and 2 integers, but I can't seem to figure out how to write the data correctly. If I am simply writing...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
9
by: JimmyKoolPantz | last post by:
IDE: Visual Studio 2005 Language: VB.NET Fox Pro Driver Version: 9.0.0.3504 Problem: I currently have a problem altering a DBF file. I do not get any syntax errors when running the program. ...
19
by: Lee Crabtree | last post by:
Is there a class in the framework that allows me read text from a file in an unbuffered manner? That is, I'd like to be able to read lines in the same manner as StreamReader.ReadLine(), but I also...
4
by: MikeJ | last post by:
make a While loop ofs = TextFileServer("somefile") string srow while (ofs=false) { srow=ofs.getRow(); Console.Writeline(srow); }
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.