Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: C# Help

  1. #1
    New Coder
    Join Date
    Sep 2006
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    C# Help

    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

    Code:
    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);
                    }
                }
            }
        }
    }

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kentucky
    Posts
    133
    Thanks
    0
    Thanked 1 Time in 1 Post
    After your loop, you'll need to flush the writer with: writer.Flush();
    Also, don't forget to Close() the writer.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •