Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. write an object of user defined class into file.

write an object of user defined class into file.

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    anilksingh
    wrote on last edited by
    #1

    How to write and read an object of user defined class say ABC into file.

    C S 2 Replies Last reply
    0
    • A anilksingh

      How to write and read an object of user defined class say ABC into file.

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Depends what you are using. Are you using the MFC's ? If yes you can look at serialization. And please, be more specific when you ask a question.

      1 Reply Last reply
      0
      • A anilksingh

        How to write and read an object of user defined class say ABC into file.

        S Offline
        S Offline
        Sarvan AL
        wrote on last edited by
        #3

        Hi anilksingh, I used to write objects of my class, in the C way, using "fwrite". Here is the code snippet: Regards, Sarvan AL class Student { int regno; char name[30] ; public: Student() { regno = 0; strcpy(name, "NULL") ; } Student(int rno, char *tname) { regno = rno ; strcpy(name, tname) ; } void Display() { cout<<"Regno: " << rno << endl ; cout<<"Name: " << name << endl ; } } ; main() { FILE *fp = fopen("Test.dat", "wb") ; Student s1(1, "Robert"), s2(2, "Smith") ; fwrite(&s1, sizeof(Student), 1, fp) ; fwrite(&s2, sizeof(Student), 1, fp) ; fclose(fp) ; Student t ; fp = fopen("Test.dat", "rb") ; fread(&t, sizeof(Student), 1, fp) ; t.Dispaly() ; fread(&t, sizeof(Student), 1, fp) ; t.Dispaly() ; fclose(fp) ; }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups