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. replacing new lines and tab spacing with a single space

replacing new lines and tab spacing with a single space

Scheduled Pinned Locked Moved C#
helpquestion
7 Posts 5 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.
  • M Offline
    M Offline
    mist_psycho
    wrote on last edited by
    #1

    I am passing a text file using streamreader and filestream.......My moto is to replace the new lines and tab spacing with a single space....But instead the resulting text file is empty once my project is execute......... plz help !!! using System.IO; . . . . . string path="C://test.txt"; Streamreader re=File.Openext(path); StreamWriter rw=new StreamWriter(path); while((input=re.ReadAllLines())!=null) { if(input=="\n") rw.Write(" "); if(input=="\t") rw.Write(" "); } .......whr am i goin wrong ???

    A R P 3 Replies Last reply
    0
    • M mist_psycho

      I am passing a text file using streamreader and filestream.......My moto is to replace the new lines and tab spacing with a single space....But instead the resulting text file is empty once my project is execute......... plz help !!! using System.IO; . . . . . string path="C://test.txt"; Streamreader re=File.Openext(path); StreamWriter rw=new StreamWriter(path); while((input=re.ReadAllLines())!=null) { if(input=="\n") rw.Write(" "); if(input=="\t") rw.Write(" "); } .......whr am i goin wrong ???

      A Offline
      A Offline
      agent00zelda
      wrote on last edited by
      #2

      One suggestion would be to check out the members of the StreamReader and StreamWriter classes, along with sample code. Try using Read() or ReadLine() instead of the ReadAllLines() in your while loop condition. http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx[^] http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx[^] Also, you might want to consider copying the data as you read it to a new text file and overwriting the old one when you are finished. And don't forget to close the filestreams when complete.

      modified on Monday, April 20, 2009 10:49 AM

      1 Reply Last reply
      0
      • M mist_psycho

        I am passing a text file using streamreader and filestream.......My moto is to replace the new lines and tab spacing with a single space....But instead the resulting text file is empty once my project is execute......... plz help !!! using System.IO; . . . . . string path="C://test.txt"; Streamreader re=File.Openext(path); StreamWriter rw=new StreamWriter(path); while((input=re.ReadAllLines())!=null) { if(input=="\n") rw.Write(" "); if(input=="\t") rw.Write(" "); } .......whr am i goin wrong ???

        R Offline
        R Offline
        riced
        wrote on last edited by
        #3

        1 You should output to a new file not the one you are reading in. For example use:

        string path="C://test.txt";
        Streamreader re=File.Openext(path);
        StreamWriter rw=new StreamWriter(@"C:\test_out.txt"));

        2 Your loop is wrong. Look at what ReadAllLines() does in help. You probably want something like:

        string input;
        StringBuilder sb = new StringBuilder("");
        while((input=re.ReadLine())!=null)
        {
        input = input.Replace('\t', ' '); //replace tabs
        sb.Append(input + " ");
        }
        rw.Write(sb.ToString());

        The StringBuilder is used because ReadLine() drops the new lines (so you don't need to replace them) and you need to add the space after the line. 3 Have a look at File.ReadAllText() and Replace() for strings. It might be simpler to use these.

        Regards David R

        M 1 Reply Last reply
        0
        • R riced

          1 You should output to a new file not the one you are reading in. For example use:

          string path="C://test.txt";
          Streamreader re=File.Openext(path);
          StreamWriter rw=new StreamWriter(@"C:\test_out.txt"));

          2 Your loop is wrong. Look at what ReadAllLines() does in help. You probably want something like:

          string input;
          StringBuilder sb = new StringBuilder("");
          while((input=re.ReadLine())!=null)
          {
          input = input.Replace('\t', ' '); //replace tabs
          sb.Append(input + " ");
          }
          rw.Write(sb.ToString());

          The StringBuilder is used because ReadLine() drops the new lines (so you don't need to replace them) and you need to add the space after the line. 3 Have a look at File.ReadAllText() and Replace() for strings. It might be simpler to use these.

          Regards David R

          M Offline
          M Offline
          mist_psycho
          wrote on last edited by
          #4

          my input in the text file is somethin like : hi this is mist I need the output as : hi this is mist ...... i tried using ur tips for removing the new lines and replace them with a single space but could'nt do....and how can i get the output in d same text file only ?? need help..... !!..plz

          OriginalGriffO R 2 Replies Last reply
          0
          • M mist_psycho

            my input in the text file is somethin like : hi this is mist I need the output as : hi this is mist ...... i tried using ur tips for removing the new lines and replace them with a single space but could'nt do....and how can i get the output in d same text file only ?? need help..... !!..plz

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5
                    StringBuilder sb = new StringBuilder();
                    string line;
                    using (StreamReader sr = new StreamReader(fileName))
                        {
                        while ((line = sr.ReadLine()) != null)
                            {
                            sb.Append(line + " ");
                            }
                        }
                    line = sb.ToString();
                    // Do what you need with the output data.
            

            Now, read a manual, and do your own homework next time!

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            • M mist_psycho

              my input in the text file is somethin like : hi this is mist I need the output as : hi this is mist ...... i tried using ur tips for removing the new lines and replace them with a single space but could'nt do....and how can i get the output in d same text file only ?? need help..... !!..plz

              R Offline
              R Offline
              riced
              wrote on last edited by
              #6

              The code I and OriginalGriff gave should work. Unless you show us some code and tell us what it produces we can only guess at how to help you. :)

              Regards David R

              1 Reply Last reply
              0
              • M mist_psycho

                I am passing a text file using streamreader and filestream.......My moto is to replace the new lines and tab spacing with a single space....But instead the resulting text file is empty once my project is execute......... plz help !!! using System.IO; . . . . . string path="C://test.txt"; Streamreader re=File.Openext(path); StreamWriter rw=new StreamWriter(path); while((input=re.ReadAllLines())!=null) { if(input=="\n") rw.Write(" "); if(input=="\t") rw.Write(" "); } .......whr am i goin wrong ???

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                Do you want multiple whitespace characters changed to one SPACE? Or each whitespace character changed to a SPACE?

                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