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. ReadLine, Perform Action, DeleteLine, ReadNextLine...

ReadLine, Perform Action, DeleteLine, ReadNextLine...

Scheduled Pinned Locked Moved C#
7 Posts 4 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.
  • J Offline
    J Offline
    jas0n23
    wrote on last edited by
    #1

    Hi All! :-D I am currently working on some file operations for my application and I want to Read the first line of a text file, grab it in a string, perform some actions, delete that line in the text file, and then do the same thing on the second line, and then the same for every line after that until there are no more lines left. Does anybody have any advice on how to go about this? I'd appreciate any help I can get on this, thanks for reading. Jay.

    j.t.

    M D L J 4 Replies Last reply
    0
    • J jas0n23

      Hi All! :-D I am currently working on some file operations for my application and I want to Read the first line of a text file, grab it in a string, perform some actions, delete that line in the text file, and then do the same thing on the second line, and then the same for every line after that until there are no more lines left. Does anybody have any advice on how to go about this? I'd appreciate any help I can get on this, thanks for reading. Jay.

      j.t.

      M Offline
      M Offline
      Mirko1980
      wrote on last edited by
      #2

      I'm afraid there is no direct way to remove a line from the beginning of a file. The only way you can do this is to overwrite the entire file with all the lines in the original file but the first one. In case you don't know, you can use File.ReadAllLines and File.WriteAllLines to read and write lines on a file.

      1 Reply Last reply
      0
      • J jas0n23

        Hi All! :-D I am currently working on some file operations for my application and I want to Read the first line of a text file, grab it in a string, perform some actions, delete that line in the text file, and then do the same thing on the second line, and then the same for every line after that until there are no more lines left. Does anybody have any advice on how to go about this? I'd appreciate any help I can get on this, thanks for reading. Jay.

        j.t.

        D Offline
        D Offline
        Deresen
        wrote on last edited by
        #3

        To do this, you have to do something like this:

        while(true)
        {
        StringBuilder wholeText = "";
        String fileName = "file.txt";
        // create reader & open file
        Textreader tr = new StreamReader(fileName);

        // read a line of text
        String oneLine = tr.ReadLine();
        if(oneLine == null)
        {
        break;
        }
        else
        {
        doSomethingWith(oneLine);
        }

        while(String s = tr.ReadLine != null)
        {
        wholeText.add(String.Format("{0}\n",s);
        }
        // close the stream
        tr.Close();

        //open the same file and write the text in there
        Textwriter tw = new StreamWriter(fileName);
        tw.write(wholeText.ToString());
        tw.Close();
        }

        This isn't tested, but the methodoligy should work.

        1 Reply Last reply
        0
        • J jas0n23

          Hi All! :-D I am currently working on some file operations for my application and I want to Read the first line of a text file, grab it in a string, perform some actions, delete that line in the text file, and then do the same thing on the second line, and then the same for every line after that until there are no more lines left. Does anybody have any advice on how to go about this? I'd appreciate any help I can get on this, thanks for reading. Jay.

          j.t.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Why would you delete the line? in your scenario, when you are done, the file is empty. You could as well use File.ReadAllLines as has been said before, but leave the lines where they are and delete the entire file (or just all its content with WriteAllText("") when done. Much easier. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


          1 Reply Last reply
          0
          • J jas0n23

            Hi All! :-D I am currently working on some file operations for my application and I want to Read the first line of a text file, grab it in a string, perform some actions, delete that line in the text file, and then do the same thing on the second line, and then the same for every line after that until there are no more lines left. Does anybody have any advice on how to go about this? I'd appreciate any help I can get on this, thanks for reading. Jay.

            j.t.

            J Offline
            J Offline
            jas0n23
            wrote on last edited by
            #5

            Thank ya'll for your great advice. :-D It's much appreciated. I figured out how to do it Yay!!!!! Here's the code incase anybody's interested.

            private void TookMeLongEnough_Click(object sender, EventArgs e)
            {
            string[] lines = File.ReadAllLines(openFileDialog1.Tag.ToString());
            foreach (string line in lines)
            {
            SaveSessionTextBox.Text = fbd.SelectedPath +
            "\\" +
            i++ +
            "A" +
            i++ +
            DateTime.Now.Hour +
            DateTime.Now.Minute +
            DateTime.Now.Second +
            DateTime.Now.Millisecond +
            ".spr";
            }
            }

            foreach( inch on Jason ) { Girlfriend.IsHappier(); }

            L 1 Reply Last reply
            0
            • J jas0n23

              Thank ya'll for your great advice. :-D It's much appreciated. I figured out how to do it Yay!!!!! Here's the code incase anybody's interested.

              private void TookMeLongEnough_Click(object sender, EventArgs e)
              {
              string[] lines = File.ReadAllLines(openFileDialog1.Tag.ToString());
              foreach (string line in lines)
              {
              SaveSessionTextBox.Text = fbd.SelectedPath +
              "\\" +
              i++ +
              "A" +
              i++ +
              DateTime.Now.Hour +
              DateTime.Now.Minute +
              DateTime.Now.Second +
              DateTime.Now.Millisecond +
              ".spr";
              }
              }

              foreach( inch on Jason ) { Girlfriend.IsHappier(); }

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              some remarks anyway: 1.

              jas0n23 wrote:

              openFileDialog1.Tag.ToString()

              very strange, you use the Tag to hold a filespec? 2. the hour/minute/sec/msec parts would be nicer with a fixed width, i.e. leading zeroes. Try DateTime.Now.ToString("HHmmssfff") 3. not sure why you would want two autoincrements in the filename; I would suggest

              s=fbd.SelectedPath + "\\"+ i.ToString("N5")+ "_" + DateTime.Now.ToString("HHmmssfff")+".spr";
              i++;

              :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


              J 1 Reply Last reply
              0
              • L Luc Pattyn

                some remarks anyway: 1.

                jas0n23 wrote:

                openFileDialog1.Tag.ToString()

                very strange, you use the Tag to hold a filespec? 2. the hour/minute/sec/msec parts would be nicer with a fixed width, i.e. leading zeroes. Try DateTime.Now.ToString("HHmmssfff") 3. not sure why you would want two autoincrements in the filename; I would suggest

                s=fbd.SelectedPath + "\\"+ i.ToString("N5")+ "_" + DateTime.Now.ToString("HHmmssfff")+".spr";
                i++;

                :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                J Offline
                J Offline
                jas0n23
                wrote on last edited by
                #7

                Haha, yeah now that you mention it Luc Pattyn, it is very strange that I use .Tag to hold File Specs. But, it's just a weird preference is all. Is there any reason as to why this may be a bad thing to do? Regards, Jay.

                foreach( inch on Jason ) { Girlfriend.IsHappier(); }

                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