Thread: Question about files

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    10

    Question about files

    I am working on a game where I had too much global data. My current solution is to write the data to a file and then to read it back. Here is some code:

    fstream f;
    f.open("\\screens.txt", ios::in | ios:: out | ios::binary);
    ...
    int screen0[20][20] = {...
    for (int i = 0; i < 20; i++)
    for (int j = 0; j < 20; j++)
    f.write((unsigned char*)screen0[j][i], sizeof(int));
    for (int i = 0; i < 20; i++)
    for (int j = 0; j < 20; j++)
    f.read((unsigned char*)screen[j][i], sizeof(int));

    I have multiple arrays for each screen in the game (screen0, screen1...), and depending on a condition, I want to make the current screen a copy of that certain array. I do this by putting the selected screen in a file and then reading it out as the screen array.

    I know it sound kinda wierd, but I have tried a lot of things and nothing has worked yet. The problem with this is that I get error messages in the linking process, such as
    Undefined symbol filebuf:: openprot in module.
    ...

    Does anyone have an idea what might be causing this? I would greatly appreciate any help you can offer.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Does anyone have an idea what might be causing this?
    Did you #include <fstream>? You would need to include this in each file which needs the file streams.
    Last edited by swoopy; 11-15-2007 at 05:22 PM.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    Quote Originally Posted by swoopy View Post
    >Does anyone have an idea what might be causing this?
    Did you #include <fstream>? You would need to include this in each file which needs the file streams.
    I have included it, and it still gives me those errors. But wouldn't it give me a compiler error anyway?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Works for me on cygwin, which compiler are you using?
    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    int main ( ) {
        fstream f;
        f.open( "foo.bin", ios::in | ios:: out | ios::binary | ios::trunc );
        char msg[10] = "woohoo";
        f.write( msg, 10 );
        char test[10];
        f.seekg( 0, ios::beg );
        f.read( test, 10 );
        cout << test << endl;
        f.close();
    }
    > f.write((unsigned char*)screen0[j][i], sizeof(int));
    1. It should be &screen0[j][i]
    2. You could write out the whole array in one go with
    f.write( (unsigned char*)screen0, sizeof screen0 );
    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.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    Thanks for responding. What does ios::trunc mean? and seekg?

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The trunc flag means the file should be truncated to zero length on opening.

    The seekg sets the read position.

    You should look up stuff like that in a reference.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question on reading files in C
    By TaiL in forum C Programming
    Replies: 12
    Last Post: 10-05-2008, 09:48 PM
  2. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  3. input/output files question
    By ssjnamek in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2005, 12:38 PM
  4. Question about ".o" files
    By Jez_Master in forum C++ Programming
    Replies: 1
    Last Post: 04-11-2002, 01:54 AM
  5. Using files: Wacked Question
    By Denethor2000 in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2002, 12:54 AM