www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/05/02/00:13:01

Xref: news2.mv.net comp.os.msdos.djgpp:3316
From: nate AT millcomm DOT com (nate)
Newsgroups: comp.os.msdos.djgpp
Subject: gxx bug? binary files
Date: 1 May 1996 03:25:08 GMT
Organization: Millennium Communications, Inc.
Lines: 126
Message-ID: <4m6lik$h8g@misery.millcomm.com>
NNTP-Posting-Host: mill2.millcomm.com
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp


        The example program at the end of this message is a generic
example of binary file creation, reading writing and modification with
an fstream declared with ios::bin or ios::binary (depending on the
compiler).  All of the compilers produced the expected result with the 
exception of DJGPP.  Anyone have any ideas why?  Example program
execution and source code is printed at the end of this message.

details:
	- denotes unsuccessful test	+ denotes unsuccessful test
DOS v6.22 Tests:
        - DJ Delorie's DJGPP v2   (GNU 2.7.2)
                with CW Sandmann's     CWSDPMI v.90+
        + Borland's Borland C++ v3.1
UNIX Tests:
        Linux v.1.3.96 i586:
                + GNU gcc/g++ v2.7.2
        SunOS v.4.1.3_U1 9 sun4c
                + GNU gcc/g++ cygnus-2.3.3
        Solaris v5.5 sun4d sparc SUNW,SPARCserver-1000:
                + Sun's CC compiler (unknown version)
                + gcc/g++ v2.7.2


Linux example execution:

person.dat should be 800 bytes
25::  data1: 325	data2: 325
50::  data1: 1275	data2: 1275
75::  data1: 2850	data2: 2850
25(should be same as above)::  data1: 325	data2: 325
50(modified-should be 66)::  data1: 66	data2: 66
75(should be same as above)::  data1: 2850	data2: 2850


DJGPP gxx example execution:
person.dat should be 800 bytes
25::  data1: 325	data2: 325
50::  data1: 1275	data2: 1275
75::  data1: 2850	data2: 2850
25(should be same as above)::  data1: 270540832	data2: 0
50(modified-should be 66)::  data1: 66	data2: 66
75(should be same as above)::  data1: 0	data2: 0


source:


	//	djgpp-test.cc		//
	//	nate AT millcomm DOT com	//

#include <iostream.h>
#include <fstream.h>

#define FILE_NAME "person.dat"

class Person	{
public:
	Person(int a, int b)	{ data1=a; data2=b;	}
	void display(void)	{
		cout << "data1: " << data1 << "	" << "data2: " << data2;
		cout << endl;
	}
	void reset(void)	{ data1=0; data2=0;	}
	void modify(int a)	{ data1+=a; data2+=a;	}
private:
	int data1, data2;
};

main(void)	{

    Person temp_person(99,99); temp_person.reset();

  //write 100 binary objects to a file
    fstream temp_file(FILE_NAME, ios::out|ios::binary);
    for(int i=0; i<100; i++)    {
		temp_person.modify(i);
		temp_file.write((unsigned char*)&temp_person,sizeof(Person));
	}
    temp_file.close();
    cout << FILE_NAME << " should be ";
	cout <<  (sizeof(Person)*100) << " bytes" << endl;

  //display objects 50, 25 & 75 from the file
    temp_file.open(FILE_NAME, ios::in|ios::binary|ios::nocreate);
    temp_file.seekg(25*sizeof(Person));
    temp_person.reset();
    temp_file.read((unsigned char*)&temp_person,sizeof(Person));
    cout << "25::  "; temp_person.display();
    temp_file.seekg(50*sizeof(Person));
    temp_person.reset();
    temp_file.read((unsigned char*)&temp_person,sizeof(Person));
    cout << "50::  "; temp_person.display();
    temp_file.seekg(75*sizeof(Person));
    temp_person.reset();
    temp_file.read((unsigned char*)&temp_person,sizeof(Person));
	cout << "75::  ";  temp_person.display();
	temp_file.close();

  //modify temp_person (make it 66) and write it back to object 50 in file
    temp_file.open(FILE_NAME, ios::in|ios::out|ios::binary|ios::nocreate);
    temp_person.reset();
    temp_person.modify(66);
	temp_file.seekp(50*sizeof(Person));
    temp_file.write((unsigned char*)&temp_person,sizeof(Person));
	temp_file.close();

   //display objects 50, 25 & 75 from the file, 25 & 75 should be the same
   //50 should be modified
    temp_person.reset();
    temp_file.open(FILE_NAME, ios::in|ios::binary|ios::nocreate);
    temp_file.seekg(25*sizeof(Person));
    temp_person.reset();
    temp_file.read((unsigned char*)&temp_person,sizeof(Person));
    cout << "25(should be same as above)::  "; temp_person.display();
    temp_file.seekg(50*sizeof(Person));
    temp_person.reset();
    temp_file.read((unsigned char*)&temp_person,sizeof(Person));
    cout << "50(modified-should be 66)::  "; temp_person.display();
    temp_file.seekg(75*sizeof(Person));
    temp_person.reset();
    temp_file.read((unsigned char*)&temp_person,sizeof(Person));
    cout << "75(should be same as above)::  ";  temp_person.display();
	temp_file.close();
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019