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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. fread() for file system

fread() for file system

Scheduled Pinned Locked Moved C / C++ / MFC
performancehelplearning
2 Posts 2 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.
  • H Offline
    H Offline
    hrishiS
    wrote on last edited by
    #1

    Hi to All, I have some problem in Logic, while reading a file (which has many objects of structure A,)...and buffering it to a dynamically allocated memory... What I am doing exactly is that....I need to read the file...and if there is any data available, then put it to a new structure (dynamical allocated memory)... Problem is ..<b>where do I declare/allocate memory for the structure</b>, I cannot declare in beginning of the loop, because fread() will overwrite into the same memory location... something like.... A     *lpA     =     new A; while(fread(lpA,sizeof(lpA),1,ptr_myfile)     !=     EOF) {    ...do something(Add the pointer into a List) } Similar problem with declaring it inside the loop... fread() needs the first parameter...

    ----------------------------- I am a beginner

    CPalliniC 1 Reply Last reply
    0
    • H hrishiS

      Hi to All, I have some problem in Logic, while reading a file (which has many objects of structure A,)...and buffering it to a dynamically allocated memory... What I am doing exactly is that....I need to read the file...and if there is any data available, then put it to a new structure (dynamical allocated memory)... Problem is ..<b>where do I declare/allocate memory for the structure</b>, I cannot declare in beginning of the loop, because fread() will overwrite into the same memory location... something like.... A     *lpA     =     new A; while(fread(lpA,sizeof(lpA),1,ptr_myfile)     !=     EOF) {    ...do something(Add the pointer into a List) } Similar problem with declaring it inside the loop... fread() needs the first parameter...

      ----------------------------- I am a beginner

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      If you're using C++, an option maybe usign STL constainers, for instance a vector:

      std::vector <A> v;
      A a;

      while (fread(&a, sizeof(a), 1, ptr_myfile) != EOF)
      {
      v.push_back(a);
      }

      :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      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