jlimited
09-11-2008, 01:43 PM
Hello,
I am new to C# and I am having some problems with an program that I have written using from code snippets from other websites. My source file has 6000+ rows of data. I want the program to search each line within the source file. When it finds a row that contains "_DS_TIME=": 1) I want to split the line into an array, 2) locate and convert the 4th and 5th segments of that array to and INT32, 3) I then want to add 86400 to those values, 4) followed by creating a new string that contains the same content as the old row, but replacing the 4th and 5th values with the new values (old value + 86400), 5) then I want to write this new line to a different text file.
If the loop does not find the "_DS_TIME=" within the row, then I write the entire row to the new text file with no changes.
When I run this program, it cuts off several rows of data that are in the source file, but not in the new text file.
Thanks
jlimited
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace AutoDateTimePMMVTest1
{
class Program
{
static void Main(string[] args)
{
FileStream stream = new FileStream(@"C:\Documents and Settings\g032753\Desktop\test.ipj", FileMode.Open);
StreamReader reader = new StreamReader(stream);
StreamWriter writer = new StreamWriter(@"c:\Documents and Settings\g032753\Desktop\test2.ipj");
string line;
Int32 starttime;
Int32 endtime;
string newline;
while ( ( line = reader.ReadLine()) != null )
{
if (line.Contains("_DS_TIME="))
{
string[] exploded = line.Split(',');
starttime = Convert.ToInt32(exploded[3]);
endtime = Convert.ToInt32(exploded[4]);
starttime += 86400;
endtime += 86400;
newline = exploded[0] + ',' + exploded[1] + ',' + exploded[2] + ',' + starttime + ',' + endtime + ',' + exploded[5];
writer.WriteLine(newline);
}
else
{
writer.WriteLine(line);
}
}
}
}
}
I am new to C# and I am having some problems with an program that I have written using from code snippets from other websites. My source file has 6000+ rows of data. I want the program to search each line within the source file. When it finds a row that contains "_DS_TIME=": 1) I want to split the line into an array, 2) locate and convert the 4th and 5th segments of that array to and INT32, 3) I then want to add 86400 to those values, 4) followed by creating a new string that contains the same content as the old row, but replacing the 4th and 5th values with the new values (old value + 86400), 5) then I want to write this new line to a different text file.
If the loop does not find the "_DS_TIME=" within the row, then I write the entire row to the new text file with no changes.
When I run this program, it cuts off several rows of data that are in the source file, but not in the new text file.
Thanks
jlimited
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace AutoDateTimePMMVTest1
{
class Program
{
static void Main(string[] args)
{
FileStream stream = new FileStream(@"C:\Documents and Settings\g032753\Desktop\test.ipj", FileMode.Open);
StreamReader reader = new StreamReader(stream);
StreamWriter writer = new StreamWriter(@"c:\Documents and Settings\g032753\Desktop\test2.ipj");
string line;
Int32 starttime;
Int32 endtime;
string newline;
while ( ( line = reader.ReadLine()) != null )
{
if (line.Contains("_DS_TIME="))
{
string[] exploded = line.Split(',');
starttime = Convert.ToInt32(exploded[3]);
endtime = Convert.ToInt32(exploded[4]);
starttime += 86400;
endtime += 86400;
newline = exploded[0] + ',' + exploded[1] + ',' + exploded[2] + ',' + starttime + ',' + endtime + ',' + exploded[5];
writer.WriteLine(newline);
}
else
{
writer.WriteLine(line);
}
}
}
}
}