🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by Guishen Wang Wed, 30 Sep 1992 05:33
Read and write binary file
#400
Author: Guishen Wang
Date: Wed, 30 Sep 1992 05:33
73 lines
2618 bytes
 I am working on a project with G++. When I try to stream

a object into file, I find some member function of iostrem does

not  work correctly.

     First I wrote 2 objects into file then read them back.

Everything is wrong. I want know where this bug come from?

from my code ? or from the compiler?

     Please send me mail and tell me what is wrong ASAP, because I can

not  make any progress on my project before this is done.

    I appreciate your time!


    the following is the code and output:




#include <std.h>
#include <fstream.h>
#include <iostream.h>
class people{
public:
  char name[20];
  long ssn;
  people(char *n,long num){ memset (name,0,20);strcpy(name,n);}
};
int main()
{ people p0("",0);
  people p1("joe",12342);
  people p2("jery",345435);
  ofstream out("file");
  out.write((char *)&p1,sizeof(p1));
  out.write((char *)&p2,sizeof(p2));
  long pos=out.tellp();
  cout<<"position after write 2 people="<< pos<<"\n";
  out.close();

  ifstream input("file");
  input.read((char *)&p0,sizeof(p0));
  cout << "This is what is red from the file :  "<< p0.name <<" "<<p0.ssn<<"\n";
    return 0;
}



mayall% g++ -v cpptest.cc
Reading specs from /local/gnu/lib/gcc/sparc-sun-sunos4.1.1/2.1/specs
gcc version 2.1
 /local/gnu/lib/gcc/sparc-sun-sunos4.1.1/2.1/cpp -lang-c++ -v -undef -D__GNUC__=
2 -D__GNUG__=2 -D__cplusplus -Dsparc -Dsun -Dunix -D__sparc__ -D__sun__ -D__unix
__ -D__sparc -D__sun -D__unix cpptest.cc /usr/tmp/cc021848.i
GNU CPP version 2.1 (sparc)
 /local/gnu/lib/gcc/sparc-sun-sunos4.1.1/2.1/cc1plus /usr/tmp/cc021848.i -quiet
-dumpbase cpptest.cc -version -o /usr/tmp/cc021848.s
GNU C++ version 2.1 (sparc) compiled by GNU C version 2.1.
 as -o cpptest.o /usr/tmp/cc021848.s
 /local/gnu/lib/gcc/sparc-sun-sunos4.1.1/2.1/ld -e start -dc -dp /lib/crt0.o -L/
local/gnu/lib/gcc/sparc-sun-sunos4.1.1/2.1/ -L/local/gnu/lib/ cpptest.o -lg++ -l
gcc -lc -lgcc
mayall% a.out
position after write 2 people=0
This is what is red from the file :  jjoe 0


    (the position should be a number greater than 0)
    (the output "jjoe 0 "should be "joe 12342")
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads