Qbasicnews.com

Full Version: File I/O in C++
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I tried to make a procedure in C++ that reads in a file.

Code:
class _map {
    public:
        char   getMapElement(int x, int y, int z);
        void   setMapElement(int x, int y, int z, char val);
        void   loadMap(char * file);
    private:    
        int    xWide;
        int    yWide;
        int    numLayer;
        char   fld[100][100][3];
};

void _map::loadMap(char * file) {
    ifstream fin(file,ios::binary);
    fin.read((char *)(&_map::xWide), sizeof(_map::xWide));
    fin.read((char *)(&_map::yWide), sizeof(_map::yWide));
}

I got problems with line 2 and 3 of loadMap. But I don't know what's wrong. So, could you help me??
I can't program C very good but isn't the filestream object called 'fstream'?

Also, you could try using the FILE object, which is a C/C++ object.
_map is just a blueprint of a structure. You have to actually MAKE an object OF that structure.
There's some error that produce the compiler. Or some code that produce that error in runtime. It be helpful. The code seems ok to me.

But be warned, the most of library function don't deal very good with binary data.

By the way wich compiler/version are you using?
Why don't you just use a FILE pointer, fopen, fclose, fread, fget, fput, fwrite, feof, fseek and stuff?
Quote:Why don't you just use a FILE pointer, fopen, fclose, fread, fget, fput, fwrite, feof, fseek and stuff?

If you can access binary files with this, that's what I need.

BTW I use VC++ 6.0
That's because the & is taking precedence over the :: . You need to put in paranthesis:
Code:
void _map::loadMap(char * file) {
   ifstream fin(file,ios::binary);
   fin.read((char *)(& ( _map::xWide ) ), sizeof(_map::xWide));
   fin.read((char *)(& ( _map::yWide ) ), sizeof(_map::yWide));
}
I tested it and discovered this solution in MSVC 7, but it should be the same for version 6.
Quote:If you can access binary files with this, that's what I need.

And if you use fopen like so:
Code:
fopen = ("filename","rb");
r means read, b means open the file as binary.
the c style funtion are a lot better at file access anyways IMO.

and ive got a question, im not much of a c++ coder but wouldnt this work:

Code:
void _map::loadMap(char * file) {
   ifstream fin(file,ios::binary);
   fin.read((char *)(&xWide), sizeof(xWide));
   fin.read((char *)(&yWide), sizeof(yWide));
}

or instead of using _map::xWide, could you not use the this pointer?
What advantage of manually defining the scope do you get?
Quote:[quote]
the c style funtion are a lot better at file access anyways IMO.

There're people (that calls themself "purist"), that think is best not to use c function whenever there's c++ objects that to the job. (I think this way)

Quote:and ive got a question, im not much of a c++ coder but wouldnt this work:

Code:
void _map::loadMap(char * file) {
   ifstream fin(file,ios::binary);
   fin.read((char *)(&xWide), sizeof(xWide));
   fin.read((char *)(&yWide), sizeof(yWide));
}

or instead of using _map::xWide, could you not use the this pointer?
What advantage of manually defining the scope do you get?

I think there's no advantage, much more than clarity. Today compilers' optimization handle this perfectly well.

Personally i try to avoid whener is posible the "::" it's overload the clarity of code, the same with "this", unless they are really needed.

Perhaps, this clarify a little
Code:
class MyClass
{
  public:
    int SetX(int x);
  private:
    int x;
};

class OtherClass: public MyClass
{
  public:
    int SetX(int x);
};

int MyClass::SetX(int x)
{
  return this->x = x;
}

int OtherClass::SetX(int x)
{
  if(x>= 0 && x<= 10)
    return MyClass::SetX(x);
  return -1; // error code
}
Quote:That's because the & is taking precedence over the :: . You need to put in paranthesis:
Code:
void _map::loadMap(char * file) {
   ifstream fin(file,ios::binary);
   fin.read((char *)(& ( _map::xWide ) ), sizeof(_map::xWide));
   fin.read((char *)(& ( _map::yWide ) ), sizeof(_map::yWide));
}
I tested it and discovered this solution in MSVC 7, but it should be the same for version 6.

Thanks man, that works.

But stupid like I am, I've got another question.

Code:
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <stdlib.h>

// Declaration of classes

class _gfx {
    public:
        int init13h();
        int init3h();
        void pSet(short int x, short int y, unsigned char c);
};

class _npc {
    public:
        _npc();
        ~_npc() {};
        void move();    
        int    xWay[100];
        int    yWay[100];
        int    xPos[100];
        int    yPos[100];
        int    xPix[100];
        int    yPix[100];
};

class _map {
    public:
        char   getMapElement(int x, int y, int z);
        void   setMapElement(int x, int y, int z, char val);
        void   loadMap(char * file);
    private:    
        int    xWide;
        int    yWide;
        int    numLayer;
        char   fld[100][100][3];
};

class _engine {
    public:
        _map   map;
        _npc   npc;
        _gfx   gfx;
        
};

// Declaration of functions in _npc

_npc::_npc() {
    for (int n = 0; n < 100; n++) {
        _npc::xPix[n] = 0;
        _npc::xPos[n] = 0;
        _npc::xWay[n] = 0;
        _npc::yPix[n] = 0;
        _npc::yPos[n] = 0;
        _npc::yWay[n] = 0;
    }
};

void _npc::move() {
    for (int n = 0; n < 100; n++) {
        _npc::xPos[n] += _npc::xWay[n];
        _npc::yPos[n] += _npc::yWay[n];
    }
}

// Declaration of functions in _gfx

int _gfx::init13h() {
    _asm {
        mov ax,0x0013
        int 0x0010
    };
    return 0;
}

int _gfx::init3h() {
    _asm {
        mov ax,0x0003
        int 0x0010
    };
    return 0;
}

void _gfx::pSet(short int x, short int y, unsigned char c) {
    _asm {
        mov al,c
        mov ah,0x000c
        mov cx,x
        mov dx,y
        int 0x0010
    };
}

// Declaration of functions in _map

char _map::getMapElement(int x, int y, int z) {
    return _map::fld[x][y][z];
}

void _map::setMapElement(int x, int y, int z, char val) {
    _map::fld[x][y][z] = val;
}

void _map::loadMap(char * file) {
    ifstream fin(file,ios::binary);
   fin.read((char *)(& ( _map::xWide ) ), sizeof(_map::xWide));
   fin.read((char *)(& ( _map::yWide ) ), sizeof(_map::yWide));

}  

// Main

int main()
{
    _engine engine;

                engine.gfx.init13h();
                engine.gfx.pSet(160,100,15);
    
    return 0;
}
What's wrong with 'init13h','init3h','pSet'??
Please try the code out if you have a c++ compiler
Pages: 1 2