streams and files

This is a discussion on streams and files within the C++ Programming forums, part of the General Programming Boards category; I’m having some problems with displaying my file content correctly . I have a program that writes an object with ...

  1. #1
    Pupil
    Join Date
    Oct 2005
    Location
    Toledo
    Posts
    27

    Question streams and files

    I’m having some problems with displaying my file content correctly .

    I have a program that writes an object with two member items, in binary mode, to my “person.dat” file.
    Code:
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
     class person
     {
     protected:
     char name[80];
     short age;
     public:
     void getData()
         {
          cout << "Enter name: "; cin >> name;
          cout << "Enter age: "; cin >> age;
         }
     };
    int main()
    {
      person pers;
      pers.getData();
    
      ofstream outfile ("person.dat", ios::binary);
    
      outfile.write(reinterpret_cast<char*>(&pers), sizeof(pers));
    
      cout << "finished" << endl;
      system("PAUSE");
      return 0;
    }
    Output
    Enter name: Chad
    Enter age: 24
    finished


    When I use another program to read this information I get very weird results.
    Code:
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
     class person
     {
     protected:
     char name[80];
     short age;
     public:
     void showData()
         {
          cout << "Name: " << name << endl;
          cout << "Age: " << age << endl;
         }
     };
    int main()
    {
      person pers;
      //pers.getData();
    
      ifstream infile ("person.dat", ios::binary);
    
      infile.read(reinterpret_cast<char*>(&pers), sizeof(pers));
       pers.showData();
      cout << "finished" << endl;
      system("PAUSE");
      return 0;
    }
    Output
    Name: Hô|-wx;”
    Age -96
    finished


    I use DevC++ to compile my sources if that helps any

    NOTE: This is not a homework problem. The source code can be found in “Object-Oriented Programming in C++”, Robert Lafore, Page 592.
    Last edited by chad101; 10-10-2005 at 12:09 PM.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Works fine for me really. One thing you could try is keeping you class consistent across projects (even though member functions should not affect).

    Code:
    class person
    {
    protected:
    	char name[80];
    	short age;
    public:
    	void getData()
    	{
    		cout << "Enter name: "; cin >> name;
    		cout << "Enter age: "; cin >> age;
    	}
    	void showData()
    	{
    		cout << "Name: " << name << "\n";
    		cout << "Age: " << age << "\n";
    	}
    };
    Also, as this is cross project, you should make sure that they are using the same .dat file See this FAQ entry about writing struct/class objects to a file: http://www.codeguru.com/forum/showthread.php?t=269648 Make sure to close() filehandles when you're done with them too.

    >> #include <stdlib.h>

    #include <cstdlib>

  3. #3
    Pupil
    Join Date
    Oct 2005
    Location
    Toledo
    Posts
    27

    I got it!

    I deleted the ios::binary
    Code:
    ofstream outfile ("person.dat", ios::binary); 
    
    works! -> ofstream outfile ("person.dat");
    working code
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
     class person
     {
     protected:
     char name[80];
     short age;
     public:
     void getData()
         {
          cout << "Enter name: "; cin >> name;
          cout << "Enter age: "; cin >> age;
         }
     };
    int main()
    {
      person pers;
      pers.getData();
    
      ofstream outfile ("person.dat");
    
      outfile.write(reinterpret_cast<char*>(&pers), sizeof(pers));
      outfile.close();
    
      cout << "finished" << endl;
      system("PAUSE");
      return 0;
    }

    But why? Is it my compiler?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. files & streams
    By gunghomiller in forum C++ Programming
    Replies: 17
    Last Post: 08-04-2007, 11:19 PM
  2. Need help with input streams from multiple source files
    By orikon in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2005, 03:56 PM
  3. files
    By Raven Arkadon in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2005, 02:18 PM
  4. files and streams
    By correlcj in forum C++ Programming
    Replies: 6
    Last Post: 11-03-2002, 10:20 AM
  5. Dealing with i/o streams in a class
    By ender in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2002, 05:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21