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#
  4. File Manipulation

File Manipulation

Scheduled Pinned Locked Moved C#
questionannouncement
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.
  • L Offline
    L Offline
    LCI
    wrote on last edited by
    #1

    Not sure why i don't seem to get this but i would like to write some records to a .txt file. At the end of all records, would like to keep a number which signifies the count. So if there are 5 records, the last line in the file will have "5". If i have to add 10 more. I read the last line, add 10 to the number to total 15. etc. I have been using StreamReader and Writer to do this, but i find that there is a limitation I really want to be able to: Open the file Check the last line to see a number count Append more data to file, update the number count. Close the file. if (!FileExists(ReportFile)) { File.Create(ReportFile); } StreamReader stream = new StreamReader(ReportFile); //How would i get to the end of the file and only read a line that says: Count:100 where the statement "Count:" is constant, but the number will vary as we add records? Then i write the records: StreamWriter filewrite = new StreamWriter(ReportFile, true); filewrite.WriteLine(Rec); filewrite.Close; File

    P 1 Reply Last reply
    0
    • L LCI

      Not sure why i don't seem to get this but i would like to write some records to a .txt file. At the end of all records, would like to keep a number which signifies the count. So if there are 5 records, the last line in the file will have "5". If i have to add 10 more. I read the last line, add 10 to the number to total 15. etc. I have been using StreamReader and Writer to do this, but i find that there is a limitation I really want to be able to: Open the file Check the last line to see a number count Append more data to file, update the number count. Close the file. if (!FileExists(ReportFile)) { File.Create(ReportFile); } StreamReader stream = new StreamReader(ReportFile); //How would i get to the end of the file and only read a line that says: Count:100 where the statement "Count:" is constant, but the number will vary as we add records? Then i write the records: StreamWriter filewrite = new StreamWriter(ReportFile, true); filewrite.WriteLine(Rec); filewrite.Close; File

      P Offline
      P Offline
      pmarfleet
      wrote on last edited by
      #2

      Wouldn't it be easier to write the record count in a header row at the start of the file? Then you wouldn't have to read to the end of the file to retrieve this information.

      Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

      L 1 Reply Last reply
      0
      • P pmarfleet

        Wouldn't it be easier to write the record count in a header row at the start of the file? Then you wouldn't have to read to the end of the file to retrieve this information.

        Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

        L Offline
        L Offline
        LCI
        wrote on last edited by
        #3

        I actually do that: StreamReader stream = new StreamReader(QueryReportFile); buf = stream.ReadLine();//Read in th efirst line which is the count int count = (int)buf; count++; stream.Close My question here is that after i read the count and add to it, i have to update the first line with the new count. Do i have to create another StreamWriter just for that line, then execute below. How can i do this with one StreamWriter?? StreamWriter filewrite = new StreamWriter(ReportFile,true); filewrite.WriteLine(RQ); filewrite.Close();

        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