Streamreader, Stringbuilder, and Stream or Stringwriter
-
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
-
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
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
-
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
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();
-
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();
Yeah, that's it. Don't forget to dispose your
StreamWriter
andStreamReader
. And next time please wrap your code with<pre lang="C#"> ... </pre>
tagsSee my article about Windows 7 Taskbar timer here on CodeProject