Thread: Simple istream question with fin.read

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    93

    Simple istream question with fin.read

    If I have an array like so

    Code:
    Resort  R[100];
    and resNum is the number of R's instantiated
    and I wrote to a binary file like so


    Code:
    ofstream fout("resorts.bin",ios::out | ios::app | ios::binary);
    Code:
    if(fout.is_open()){
                for(int i = 0; i < ResNum; i++) fout.write((char *)&R,sizeof(R));}
    and I want to later read them back into each appropriate R[i] that I wrote them to, I need help with that.

    How would I read the binary file back into R[i]?

    using
    Code:
    for(int i = 0; i < ResNum; i++){
    fin.read((char*)&R,sizeof R);
    ......etc}
    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Is R a complex class, with derived classes, inheritance etc ?

    Because it may contain hidden information which you can write out, but it won't mean a thing if you read it in again.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    No inheritance.

    Its initially instantiated like so

    Code:
    Resort R[100] = {
                       {1267.45, 3, "Barcelo Marina Palace", "Varadero", "Cuba", "(416) 234-2345"},
                       {2159.99, 21, "Iberostar", "Varadero", "Cuba", "(416) 234-2346"},
                       {2400.26, 415, "Golden Crown Paradise Spa", "Cancun", "Mexico", "(416) 234-2444"},
                       {2899.54, 64, "Secrets Excellence All Suites Resort", "Punta Cana", "Domincan Republic", "(416) 234-2347"},
                       {1849.64, 51, "Melia Cayo Coco", "Cayo Coco", "Cuba", "(416) 234-2347"},
                       {1889.84, 15, "Aventura Spa Palace", "Mayan Riviera", "Mexico", "(416) 254-5547"},
                       {3849.04, 45, "El Dorado Seaside Suites", "Mayan Riviera", "Mexico", "(416) 254-2527"}
      };

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How did you define Resort?

    Are all those strings you have in there
    char foo[100];
    char *foo;
    std::string foo;
    ?

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    Code:
    struct Resort{
      double
        weeklyPrice;
      unsigned int
        facilities;
      char
         name[41],
         location[31],
         country[29],
         agentPhone[15];
    };

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    in this case you can use
    Code:
    for(int i = 0; i < ResNum; i++){
         fin.read((char*)&R[i],sizeof( R));
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Or even the whole thing in one call
    fin.read ( (char*)R, sizeof(R)*ResNum );

  8. #8
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    Quote Originally Posted by Salem
    Or even the whole thing in one call
    fin.read ( (char*)R, sizeof(R)*ResNum );
    Ill try something else, im getting access violation errors with that.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    You know, if you define operator>> and operator<< for your Resort struct, you can make this a WHOLE lot easier to handle. Also, unless there's a need for the char arrays (or you can't change the struct), I'd suggest using std::String instead, and leverage its capabilities.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >if(fout.is_open()){
    >            for(int i = 0; i < ResNum; i++) fout.write((char *)&R,sizeof(R));}
    For starters, this code should be:
    Code:
    if(fout.is_open()){
                for(int i = 0; i < ResNum; i++) fout.write((char *)&R[i],sizeof(R[i]));}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM