a question on classes

This is a discussion on a question on classes within the C++ Programming forums, part of the General Programming Boards category; i just learned that i could write classes to a file using the function fwrite(). (defined in stdio) and i ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    53

    Exclamation a question on classes

    i just learned that i could write classes to a file using the function fwrite(). (defined in stdio)

    and i was wondering if i could write the whole class in one of its own functions, perhaps something like this:

    Code:
    #include <stdio.h>
    
    class dot{
    private:
    	int x;
    	int y;
    	wchar_t* title;
    public:
    	void tofile(void);
    ///some other unnecessary functions///
    };
    
    void dot::tofile(void)
    {
    	FILE* file;
    file=fopen("dotfile.dat","w+");
    fwrite(&dot,sizeof(dot),1,file);    ///this is where i am not sure of
    fclose(file);
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Actually you can't write this class to file like this. Storing the address of the string in title doesn't help you retrieve the string value.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    53
    So do you have any suggestions?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    You need to learn yourself Boost Serialization:
    Serialization

    Also, do not put raw pointers in your class.
    SourceForge.net: Raw pointer issues - cpwiki

    And of course, unless you have a very good reason, don't use C file I/O.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    53
    thanks for your reply

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes question...
    By Raigne in forum C++ Programming
    Replies: 24
    Last Post: 09-12-2006, 01:46 AM
  2. Replies: 2
    Last Post: 07-28-2006, 02:59 PM
  3. Simple Question about Classes
    By Loctan in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2006, 02:40 AM
  4. Classes and Win32 API, and another Question
    By philvaira in forum Windows Programming
    Replies: 10
    Last Post: 04-10-2004, 07:21 PM
  5. Newbie question about the Private type in classes
    By Ryeguy457 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2002, 10:17 PM

Tags for this Thread


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