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. Managed C++/CLI
  4. How do you reset StreamReader? There is no seek function.

How do you reset StreamReader? There is no seek function.

Scheduled Pinned Locked Moved Managed C++/CLI
csharpc++visual-studiodata-structuresquestion
3 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.
  • B Offline
    B Offline
    BuckBrown
    wrote on last edited by
    #1

    Hi. I'm using Visual Studio 2005 C++/CLI. I have a file that has its contents read into an array, one line of the file per element of the array. There may be 10 elements to read or 10,000 elements to read. I would like to define the array size dynamically by reading each line of the file, counting until all the lines are read. Then I want to go back to the begining of the stream but there is no Seek() function. I tried streamReader->Close() and then another streamReader = gcnew StramReader(fileStream) but that doesn't work. Can I reset the streamReader back to the beginning? Thanks Buck

    R 1 Reply Last reply
    0
    • B BuckBrown

      Hi. I'm using Visual Studio 2005 C++/CLI. I have a file that has its contents read into an array, one line of the file per element of the array. There may be 10 elements to read or 10,000 elements to read. I would like to define the array size dynamically by reading each line of the file, counting until all the lines are read. Then I want to go back to the begining of the stream but there is no Seek() function. I tried streamReader->Close() and then another streamReader = gcnew StramReader(fileStream) but that doesn't work. Can I reset the streamReader back to the beginning? Thanks Buck

      R Offline
      R Offline
      Robert C Cartaino
      wrote on last edited by
      #2

      I think you can access the Seek method through the StreamReader's BaseStream property. I'm not by a C++ compiler so I can't test this. Try this out:

      FileStream* fs = new FileStream(S"YourFile.txt", FileMode::???, FileAccess::???);
      StreamReader* sr = new StreamReader(fs);

      sr->BaseStream->Seek(0, SeekOrigin::Begin);

      I think you will also need to call sr->DiscardBufferedData() after the Seek or you will get unexpected results.

      B 1 Reply Last reply
      0
      • R Robert C Cartaino

        I think you can access the Seek method through the StreamReader's BaseStream property. I'm not by a C++ compiler so I can't test this. Try this out:

        FileStream* fs = new FileStream(S"YourFile.txt", FileMode::???, FileAccess::???);
        StreamReader* sr = new StreamReader(fs);

        sr->BaseStream->Seek(0, SeekOrigin::Begin);

        I think you will also need to call sr->DiscardBufferedData() after the Seek or you will get unexpected results.

        B Offline
        B Offline
        BuckBrown
        wrote on last edited by
        #3

        Thanks Robert, Yes, that works. The key here is the sr->DiscardBufferedData() otherwise the sr->BaseStream->Seek() function does nothing. Buck

        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