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. Streamreader, Stringbuilder, and Stream or Stringwriter

Streamreader, Stringbuilder, and Stream or Stringwriter

Scheduled Pinned Locked Moved C#
database
4 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.
  • J Offline
    J Offline
    JasonLee07
    wrote on last edited by
    #1

    I have done plenty of googling and have gotten close to a solution but wanted to make sure I'm going about it the right way. I have a data file which is a non delimited fixed field position file that has missing data. I basically want to read each line in and determine if a certain field is blank. If so I use another field in that same record to retrieve the missing data from the database. After getting the missing value I would like to use StreamWriter or StringWriter (not sure which) to write out each line to a new file with the updated field value. Appreciate any advice you all may have on this subject. Thanks, Jason

    D 1 Reply Last reply
    0
    • J JasonLee07

      I have done plenty of googling and have gotten close to a solution but wanted to make sure I'm going about it the right way. I have a data file which is a non delimited fixed field position file that has missing data. I basically want to read each line in and determine if a certain field is blank. If so I use another field in that same record to retrieve the missing data from the database. After getting the missing value I would like to use StreamWriter or StringWriter (not sure which) to write out each line to a new file with the updated field value. Appreciate any advice you all may have on this subject. Thanks, Jason

      D Offline
      D Offline
      Dimitri Witkowski
      wrote on last edited by
      #2

      I would advice you to do the following: 1. Open the input file 2. Open the output file and create StreamWriter for it 3. Read data line-by-line, and for each line do the check and write to the output file You need StreamWriter. The difference: StreamWriter allows you to write contents to any stream: file, socket, etc; Stream represents writeable file, socket or other stream (this can be also a memory, or string) - it's not convenient to write strings to stream, that's why you need StreamWriter. StringWriter writes all contents to a string (in memory); StringBuilder allows you quickly build string from parts; you needn't it here

      J 1 Reply Last reply
      0
      • D Dimitri Witkowski

        I would advice you to do the following: 1. Open the input file 2. Open the output file and create StreamWriter for it 3. Read data line-by-line, and for each line do the check and write to the output file You need StreamWriter. The difference: StreamWriter allows you to write contents to any stream: file, socket, etc; Stream represents writeable file, socket or other stream (this can be also a memory, or string) - it's not convenient to write strings to stream, that's why you need StreamWriter. StringWriter writes all contents to a string (in memory); StringBuilder allows you quickly build string from parts; you needn't it here

        J Offline
        J Offline
        JasonLee07
        wrote on last edited by
        #3

        Thanks for the steps and showing the difference between the different classes. Below is what I have so far and I think it will work. My new file contained the 111222333 string for all the missing data which was the desired result. Thanks, Jason StreamReader sr = File.OpenText("C:\\Source.txt"); StreamWriter sw = File.CreateText("C:\\Test1.txt"); while (sr.Peek() != -1) { string strRecord = sr.ReadLine(); string strSurname = strRecord.Substring(0, 10); string strFirstName = strRecord.Substring(11, 8); string strPupilNum = strRecord.Substring(20, 9).Trim(); string strSSID = strRecord.Substring(373, 9).Trim(); if (strPupilNum == String.Empty) { // call db stored procedure to retrieve PupilNum based on SSID StringBuilder sb = new StringBuilder(strRecord); sb.Remove(20, 9); sb.Insert(20, "111222333", 1); strRecord = sb.ToString(); } sw.WriteLine(strRecord); } sw.Close();

        D 1 Reply Last reply
        0
        • J JasonLee07

          Thanks for the steps and showing the difference between the different classes. Below is what I have so far and I think it will work. My new file contained the 111222333 string for all the missing data which was the desired result. Thanks, Jason StreamReader sr = File.OpenText("C:\\Source.txt"); StreamWriter sw = File.CreateText("C:\\Test1.txt"); while (sr.Peek() != -1) { string strRecord = sr.ReadLine(); string strSurname = strRecord.Substring(0, 10); string strFirstName = strRecord.Substring(11, 8); string strPupilNum = strRecord.Substring(20, 9).Trim(); string strSSID = strRecord.Substring(373, 9).Trim(); if (strPupilNum == String.Empty) { // call db stored procedure to retrieve PupilNum based on SSID StringBuilder sb = new StringBuilder(strRecord); sb.Remove(20, 9); sb.Insert(20, "111222333", 1); strRecord = sb.ToString(); } sw.WriteLine(strRecord); } sw.Close();

          D Offline
          D Offline
          Dimitri Witkowski
          wrote on last edited by
          #4

          Yeah, that's it. Don't forget to dispose your StreamWriter and StreamReader. And next time please wrap your code with <pre lang="C#"> ... </pre> tags

          See my article about Windows 7 Taskbar timer here on CodeProject

          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