Thread: Write to binary file from 2D array

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    5

    Write to binary file from 2D array

    I'm trying to write to a binary file from a 1D array while searching for a specific enumerated type. What I need to write to the file are both indexes as well as the enumerated type (int value, in this case 1) I'm pretty sure this for loop isn't working and am wondering if someone can eyeball it and tell me what I am missing or if I am way off the mark..


    Code:
    status = reserved;
    
    int temp = 1;  //reserved status of 1
    int indx1 = 0;
    int indx2 = 0;  
    
    fstream numFile;
    numFile.open("PLANE.BIN", ios::binary | ios::out);
    
        for (indx1 = 0; indx1 < 15; indx1++)
           for (indx2 = 0; indx2 < 6; indx2++)
              if (seatArray[indx1][indx2] == status){
                  numFile.write( reinterpret_cast<char*>(&seatArray[indx1]), sizeof(int) );
                  numFile.write( reinterpret_cast<char*>(&seatArray[indx2]), sizeof(int) );
                  numFile.write( reinterpret_cast<char*>(&temp), sizeof(int) );
                  }
    
                  numFile.close();
    
    Thanks, 
    Bill
    Last edited by Salem; 11-28-2006 at 06:07 AM. Reason: Removed prose from the code tags

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Try to keep the explanation seperate from the C++ code, makes reading the problem simpler
    Double Helix STL

  3. #3
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Try adding curly brackets to your for loops.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    The curly braces aren't really needed since the inner for loop and the if statement are considered single statements. They do make the code look better though. Also that close() statement is badly indented giving the wrong impression as to when it will be executed.

    Anyways, my guess is you sizeof statements. You are trying to write sizeof(int) bytes of an array (2nd dimension of seatArray)

    You probably want sizeof(seatArray[indx1]).

    EDIT: Wait... you are trying to write the whole array contained in seatArray[indx1]? Or just one element, like seatArray[indx1][indx2]?
    Last edited by Mario F.; 11-27-2006 at 07:45 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    5
    Just one element..

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    seatArray[indx1] - is not an index, indx1 is index
    the same about the second line

    Are you shure you need indexes and type as int? As far as I see - 1 byte per value is enough
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > numFile.write( reinterpret_cast<char*>(&seatArray[indx1]), sizeof(int) );
    > numFile.write( reinterpret_cast<char*>(&seatArray[indx2]), sizeof(int) );

    To reinterate what's been said, writing the indices would be:
    Code:
                  numFile.write( reinterpret_cast<char*>&indx1, sizeof(int) );
                  numFile.write( reinterpret_cast<char*>&indx2, sizeof(int) );

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    5

    Read from binary file

    I need to read all data from a binary file and store it in an array, I took a stab at it with the following code but id doesn't appear to be cutting it, can someone advise please?

    Code:
    int idx1 = 0;
    int idx2 = 0;
    int temp = 0;
    
    fstream binFile;
    
    binFile.open ("PLANE.BIN", ios::binary | ios::in);
    
    
    if (binFile)
       {
    
       binFile.read(reinterpret_cast<char*>(&seatArray[idx1]), sizeof(int) );
       binFile.read(reinterpret_cast<char*>(&seatArray[idx2]), sizeof(int) );
       binFile.read(reinterpret_cast<char*>(&temp), sizeof(int) );                
    
    }
    
    else
         cout << "File PLANE.BIN not found" << endl << endl;

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    What was wrong with the other thread you started? Did you read the answers? Did you try anything? Your code looks the same to me.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Based on the other thread, it looks like what you want is something like this:
    Code:
        for (indx1 = 0; indx1 < 15; indx1++)
           for (indx2 = 0; indx2 < 6; indx2++)
               seatArray[indx1][indx2] = 0;
    
        int idx1 = 0;
        int idx2 = 0;
        int temp = 0;
    
        fstream binFile;
    
        binFile.open ("PLANE.BIN", ios::binary | ios::in);
    
        if (binFile)
        {
    
           while (binFile.read(reinterpret_cast<char*>(&idx1), sizeof(int) )
           {
                binFile.read(reinterpret_cast<char*>(&idx2), sizeof(int) );
                binFile.read(reinterpret_cast<char*>(&temp), sizeof(int) );                
                seatArray[idx1][idx2] = temp;
           }
    
        }
    
        else
             cout << "File PLANE.BIN not found" << endl << endl;
    Last edited by swoopy; 11-28-2006 at 10:40 PM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Threads merged, as per Mario's observation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Reading a file and storing it as a 2d Array.
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-08-2009, 07:33 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Replies: 4
    Last Post: 03-02-2003, 09:12 AM
  5. file into 2d array
    By lakai02 in forum C Programming
    Replies: 0
    Last Post: 12-22-2002, 04:02 PM