Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

C++ struct/fstream/array/loop problem


  • Please log in to reply

#1
Ren

Ren

    Member

  • Member
  • PipPip
  • 20 posts
This function is supposed to take in a filename and an array of structs (arrays are automatically passed by reference, right?) then it should read from the file and save each line into the correct struct in the array. Can someone please help me get it working??

void OpenAndReadFile(string Filename, house FileHouses[MAXHOUSES])
{

//create input filestream and open the file
ifstream fin(Filename.c_str() );
fin.open(Filename.c_str());


//if the file didn't open, display an error and terminate the program.
if (!fin.is_open())
{
cout << "There was an error opening the file" << endl;
system("pause");
exit(1);
}

//this for loop reads each line in the file and saves everything into the array for houses.


for (int Count = 0;Count < MAXHOUSES;Count++)
{
getline(fin,FileHouses[Count].Address);
getline(fin,FileHouses[Count].Suburb);
fin >> FileHouses[Count].Price;
}



//close the file
fin.close();


}
  • 0

Advertisements


#2
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 875 posts
here's an example on structs

http://www.cs.utsa.e.../C_structs.html

look at print_p()

as in the example above try:
void OpenAndReadFile(string Filename, house *FileHouses[MAXHOUSES])
getline(fin,(*FileHouses[Count]).Address);
getline(fin,(*FileHouses[Count]).Suburb);
fin >> (*FileHouses[Count]).Price;
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP