Reading a file

I need to read in a file that has two numbers seperated by a space on each line
1 2
3 4
5 7
123 254

I need to read these values into a struct. I can't seem to get the correct values into the struct. Any ideas?

struct physical_memory
{
      int log_address;
      int mem_address;
};


class pagemem
{
      private:
            ifstream newfile;
      public:
            pagemem();
            physical_memory *pmem;


};

pagemem::pagemem()
{
      pmem = new physical_memory[255];
      int i =0;

      newfile.open("allocated.dat", ios::in);
      if(newfile.is_open())
      {
            while(!newfile.eof())
            {
                  newfile.ignore(
                  newfile.getline((char*)&pmem, sizeof(pmem));
                  cout << pmem[1].log_address << endl;
            }
      }
}
microcoopAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dennis_georgeCommented:

try this

pagemem::pagemem()
{
     pmem = new physical_memory[255];
     int i =0;

     newfile.open("allocated.dat", ios::in);
     if(newfile.is_open())
     {
          while(!newfile.eof())
          {
                char num[20] ;
                fs >> num ;
                pmem[i].log_address = atoi(num) ;

                fs >> num ;
                arr[i++].mem_address = atoi(num) ;
       }
     }
}
0
dennis_georgeCommented:
I would rather suggest you to use vectors instead of using array of structure......
0
SirHandoCommented:
You have a text file, read it like a text file.

iostreams handles reading int, float, long etc from the stream, you dont  need to convert it.

Something like this will work

pagemem::pagemem()
{
     newfile.open("allocated.dat", ios::in);
     physical_memory    pm[255];
     int                         i = 0;
     if(newfile.is_open())
     {
          newfile >> ::skipws;
          while( newfile.good() && i<255 )
          {
               newfile >> pm[i].log_address >> pm[i].mem_address;
               //
               // Do what you want with pm[i] now.
               //
               cout << pm[i].log_address << pm[i].mem_address << endl;

               // Next Record
               ++i;
          }
     }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.