C# File Handling Problem

vinayphadke 0 Tallied Votes 263 Views Share

Hello Guys,
I was performing text file reading and writting operations and the job looked very simple as usual, yet it has not been solved successfully.
We have got a simple text file final.txt which contains data like this:-
05/18/2012 02:12:66 8HRY hjhruehr737243 YES NO u34gewryge 698 i.e each value separated by ** a space** in between.
We want the output file to be written by replacing space with pipe line character '|' with condition that the the space in time stamp value at the start (05/18/2012 02:12:66) should be retained. so output will look like this
05/18/2012 02:12:66|8HRY|hjhruehr|737243|YES|NO|u34gewryge|698

With my code I'm able to produce the output as given below:
05/18/2012
02:12:66|8HRY|hjhruehr|737243|YES|NO|u34gewryge|698

Now, this is my problem. I want every thing to be written in a single line but whichever method I implement to write file gives me such output.
My code is pasted ; can anyone guide me on this? Thanks in advance.

string tempInput;
    tempInput = File.ReadAllText(@"C:\Documents and Settings\me\Desktop\ssisTemp\final.txt");
    tempInput = tempInput.Trim();
     
    int idxDate = tempInput.IndexOf(" ");
    string strDate = tempInput.Substring(0, idxDate);
    string strCut = tempInput.Substring(idxDate + 1, tempInput.Length - idxDate - 1);
    int nextidx = strCut.IndexOf(" ");
    string strTime = strCut.Substring(0, nextidx);
    string cut2 = tempInput.Substring(idxDate + nextidx + 1, (tempInput.Length - idxDate - nextidx - 1));
    cut2 = cut2.Replace(' ', '|');
    string timeStamp = tempInput.Substring(0, idxDate + nextidx + 1);
    string stroutput = string.Concat(timeStamp, cut2);
    StreamWriter sw = File.CreateText(@"C:\Documents and Settings\me\Desktop\ssisTemp\OUTPUT.txt");
    sw.Write(stroutput);
    sw.Close();
    //MessageBox.Show(@"Please refer to file @ C:\Documents and Settings\me\Desktop\ssisTemp\OUTPUT.txt");
Momerath 1,327 Nearly a Senior Poster Featured Poster

Not sure why you are getting what you say you are getting, but your method is overly complicated. This works:

int index = tempInput.IndexOf(' ');
index = tempInput.IndexOf(' ', index + 1);
String stroutput = tempInput.Substring(0, index) + tempInput.Substring(index).Replace(' ', '|');

And since the date/time is in a specific format, you could even do

String stroutput = tempInput.Substring(0, 19) + tempInput.Substring(19).Replace(' ', '|');
gnath 0 Newbie Poster

Hi Friend, I think this will be an easy one,

 public string RemoveSpecialChars(string str)
        {
            string[] chars = new string[] { ",", ".", "/", "!", "@", "#", "$", "%", "^", "&", "*", "'", "\"", ";", "-", "_", "(", ")", ":", "|", "[", "]", " ", "?" };
            //string[] charss = new string[] {"Comma","FullStop","" };
            for (int i = 0; i < chars.Length; i++)
            {
                if (str.Contains(chars[i]))
                {
                    str = str.Replace(chars[i], "|");
                }
            }
            return str;
        }


    after replacing them with that just do
    string.trimend and string.trimstart. . .
    it's all over.. . .
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.