Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
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
  • 876 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