2

I am trying to import data from a file in my project but I am having trouble finding EOF. Firstly, I used the EOF function as a condition but I after reading this, I tried changed the code but still it is giving same error. Please help me out. Thanks

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Rooms;
class Guest;
class MeetingRoomGuest;
Rooms* r_ptr[999];
int r_count=0;

ofstream infile("new.txt",ofstream::binary);
while(infile.read((char *)(&r_ptr[r_count]),sizeof(Rooms)))
{   
    r_count++;
}
infile.close(); 
int main ()
{
    // some code here
    return 0;
}

ERROR:

error C2059: syntax error : 'while'

UPDATE: Please let me know if this is a better implementation?Thanks

int main()
{
    r_ptr[r_count]= new Rooms;
    while(infile.read((&r_ptr[r_count]),sizeof(Rooms)))
    {   
        r_ptr[++r_count]= new Rooms;
        r_count++;
    }
    infile.close(); 
    //some code here
}

I am still getting an error,

ERROR:

error C2039: 'read' : is not a member of 'std::basic_ofstream<_Elem,_Traits>'

UPDATE: Thanks alot. The code has finally fixed,here is the final implementation,

int main()
{
    r_ptr[r_count]= new Rooms;
    while(infile.read((char *)(&r_ptr[r_count]),sizeof(Rooms)))
    {   
        r_count++;
        r_ptr[r_count]= new Rooms;

    }
    infile.close(); 
    // some work
}
Community
  • 1
  • 1
  • 1
    After you get past the syntax and compilation problems, there's a serious design problem: trying to read or write raw pointers as `char[]` will never do what you want. Read about serialization. – aschepler Jun 09 '11 at 11:35
  • @aschepler,Oh! Sorry, I almost forgot to allocate memory for them. Thankyou :) –  Jun 09 '11 at 11:37
  • you're incrementing r_count one to many times in the loop. try - r_ptr[r_count]= new Rooms; and this line r_ptr[r_count]= new Rooms; before the while loop is unnecessary – daven11 Jun 09 '11 at 11:47
  • @daven11 : the line before while() does not seem unnecessary to me as I need to have memmory allocated before I can use it. I also found out that I need to swap the two statements in the loop. –  Jun 09 '11 at 11:52
  • @JustAnotherProgrammer: You're right of course - my mistake – daven11 Jun 09 '11 at 12:07

2 Answers2

3

You can't have a while outside main

int main ()
{
    ofstream infile("new.txt",ofstream::binary);
    while(infile.read((char *)(&r_ptr[r_count]),sizeof(Rooms)))
    {   
        r_count++;
    }
    infile.close(); 

    // some code here
    return 0;
}
daven11
  • 2,905
  • 3
  • 26
  • 43
3

It seems you have one or two syntax problems:

  1. you've got code outside of a function or main (the while and infile commands) they need to be put into main or a function.

  2. your second while needs a do (do{....}while(1), also it runs forever

  3. All your variables are defined outside of main. This makes them global variables, a thing to be avoided as much as possible. They should be moved inside main as well

  4. ofstream are used to Output to a file, you want a ifstream to get Input from a file

Phil
  • 1,110
  • 1
  • 9
  • 25
  • Thanks. But the I am still getting the an error,how do I convert a double pointer into a char pointer?: 'std::basic_istream<_Elem,_Traits>::read' : cannot convert parameter 1 from 'Rooms **' to 'char *' –  Jun 09 '11 at 12:00
  • It's because `(char *)(&r_ptr[r_count])` tells the program to change r_ptr (a room*) into a char* and it has no idea how to do that. Reading and writing classes directly to/from files is normally a bad idea. If you really have to then @aschepler is right - google serialization – Phil Jun 09 '11 at 12:17