Click to See Complete Forum and Search --> : Reading a txt file into an array of structs problem


Swerve
March 17th, 2008, 09:00 PM
Hi!

I've wrote a program which placed the contents of three array's into a txt file.


#include <iostream>
#include <fstream>

using namespace std;
const int MAXCHARS = 20;
const int MAXITEMS = 10;



int main()
{
char ItemName[MAXITEMS][MAXCHARS] = { "Caviar", "Sprouts", "Salmon", "Eggs", "Truffles",
"Quail", "Champagne", "Bread", "Brioche", "Apples"};
double Price [MAXITEMS] = {12.90, 0.80, 6.50, 0.75, 7.29, 5.55, 21.90, 0.80, 1.20, 1.10};
bool Luxury [MAXITEMS] = {false, false, true, false, true, true, false, false, true, false};


ofstream mystream("myfile2.txt");//create and open a file called 'myfile'through a stream 'mystream'.

if(!mystream)//if the above fails, cout the text
{
cout << "phail " << endl;
}

for(int i = 0;i<MAXITEMS;i++)
{
mystream << ItemName[i] << " " << Price[i] << " " << Luxury[i] << endl;
}

mystream.close();//close the stream 'mystream' and it's associated file ('myfile').


system ("pause");

return 0;
}


The 3 arrays held:-

char string (food's name)
double (food's price)
bool (is food's classed as 'luxury')

(A total of 10 different items, image of the txt file is attached).

Now the next step is reading the txt file into an array of structs, and this is where I'm having difficulty.

What I have so far:-


#include <iostream>
#include <fstream>

using namespace std;
const int MAXCHARS = 20;
const int MAXITEMS = 10;

struct mystruct
{
char itemname[MAXCHARS];
double price;
bool luxury;
};

int main()
{
mystruct mystructname;

mystruct thestructsarrayname[MAXITEMS];

ifstream mystream("myfile2.txt");

if(!mystream)
{
cout << "phail " << endl;
}

for(int i = 1;i < 10;i++)
{
cout >> thestructsarrayname[i].price;
}

mystream.close();

system ("pause");

return 0;
}


I've been trying to do this for two days solid now (seriously) and whilst I know it's easy to some people, I'm finding it hard to figure out as I've never done it before, and whenever I ask people for help they either say something I don't understand, or go wayy over my head saying use vectors, or overload this and that.

If anyone could PLEASE just help me out with reading from the txt file so that the first food item goes into the first struct I would be EXTREMLY grateful. I appreciate just giving someone the answers isn't helpful, but being stuck with one issue for this long is not helpful either. I know my code may not be 'optimised', or have other issues I'm not aware of, but that's because I'm new to all this and trying to teach myself.

Sorry for the long post, but I've become really frustrated with this, because I know that once I do have a working program, I'll be able to go through it slowly with the debugger step by step, and that way I'll get it into my head, it's just the way my brain works.

Many many thanks to anyone who can help me with this.


Swerve :)

P.S. I've attached a screen shot of the txt file, because I don't understand how it knows which bit to read.

Paul McKenzie
March 17th, 2008, 09:38 PM
Now the next step is reading the txt file into an array of structs, and this is where I'm having difficulty.

What I have so far:-


//...
for(int i = 1;i < 10;i++)
//...

1) Arrays start at 0, not 1.

2) I don't see how you can have such a problem.

mystream << ItemName[i] << " " << Price[i] << " " << Luxury[i] << endl;

This is how you wrote to the file, so how do you read from the file? You just use operator >>.

mystream >> thestructarrayname[i].ItemName >>
thestructarrayname[i].price >> thestructarrayname[i].luxury;

I've been trying to do this for two days solid now (seriously) and whilst I know it's easy to some people, I'm finding it hard to figure out as I've never done it before,And you found no books, nothing, on this very basic concept??
P.S. I've attached a screen shot of the txt file, because I don't understand how it knows which bit to read.If you had to read in from the keyboard, it is no different than if you read one line of the file.

Regards,

Paul McKenzie

Swerve
March 18th, 2008, 06:13 PM
thanks Paul!

I have managed to read the first item into a struct:-


#include <iostream>
#include <fstream>

using namespace std;

struct items
{
char itemname[10];
double price;
bool luxury;
};

int main()
{
items structname;
char arrayname[10];
ifstream myfile("myfile2.txt");

if(!myfile)
{
cout << "phail " << endl;
}

myfile >> structname.itemname;
myfile >> structname.price;
myfile >> structname.luxury;


myfile.close();
system ("pause");
return 0;
}


But I'm wanting to enter all the items from the txt file into an ARRAY of STRUCTS.

If anyone can offer me some advice, you would be a great help!

Thanks.

Paul McKenzie
March 18th, 2008, 06:50 PM
myfile >> structname.itemname;
myfile >> structname.price;
myfile >> structname.luxury;
This could be done in a single line, not three seperate lines.

myfile >> structname.itemname >> structname.price >> structname.luxury;


But I'm wanting to enter all the items from the txt file into an ARRAY of STRUCTS.You write a loop, just like you did when you were saving the data.

I think your problem has less to do with reading into an array of struct, and more into what an array of struct is and how to manipulate one, regardless of whether you're reading or not.

Do you know how to declare an array of structs? If so, do you know how to access, say the third element of this array, and say, a member called "name"?

struct foo
{
char name[30];
int age;
};

int main()
{
foo myNames[10]; // an array of 10 foo;s
}

Similarly, what does this loop do?

for (int i = 0; i < 10; ++i)
{
foo[i].age = 20;
}

So if you know the answers to this, then I don't see why the file read is such a problem.

Regards,

Paul McKenzie