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. Text file handling issue

Text file handling issue

Scheduled Pinned Locked Moved C#
helpdata-structurestutorialquestion
6 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.
  • A Offline
    A Offline
    Andrew Em
    wrote on last edited by
    #1

    Hi I got a problem with the project i'm building, i'm using StreamWriter and to append text to a textfile, so the text file look like this: entry1 entry2 entry3 *blank row* entry4 entry5 entry6 *blank row* i do that with this code: StreamWriter sw = File.AppendText("test.txt"); sw.WriteLine(tb1.Text); sw.WriteLine(tb2.Text + " : " + tb21.Text); sw.WriteLine(tb3.Text); sw.WriteLine(); sw.Flush(); sw.Close(); Then i use a loop and array to read those files into my application. TextReader TR = new StreamReader("test.txt"); int NumberOfLines = 20; string[] ListLines = new string[NumberOfLines]; for (int i = 1; i < NumberOfLines; i++) { ListLines[i] = TR.ReadLine(); } Now i got the problem, i need to be able to delete/change the "packs" between the blank line, was thinking something like making a backup of the file and re-writing it, without the selected pack (using a listbox to select) or in some way replace that pack with what the user types in instead when i hit the delete or change button. The problem is i dont know how to get the "new" text to the same position as the old when i change it. Anyone got an idea? Sorry if i confuse you :) --

    Light is faster than sound... Thats why some people look smart till they start to talk.

    C B 2 Replies Last reply
    0
    • A Andrew Em

      Hi I got a problem with the project i'm building, i'm using StreamWriter and to append text to a textfile, so the text file look like this: entry1 entry2 entry3 *blank row* entry4 entry5 entry6 *blank row* i do that with this code: StreamWriter sw = File.AppendText("test.txt"); sw.WriteLine(tb1.Text); sw.WriteLine(tb2.Text + " : " + tb21.Text); sw.WriteLine(tb3.Text); sw.WriteLine(); sw.Flush(); sw.Close(); Then i use a loop and array to read those files into my application. TextReader TR = new StreamReader("test.txt"); int NumberOfLines = 20; string[] ListLines = new string[NumberOfLines]; for (int i = 1; i < NumberOfLines; i++) { ListLines[i] = TR.ReadLine(); } Now i got the problem, i need to be able to delete/change the "packs" between the blank line, was thinking something like making a backup of the file and re-writing it, without the selected pack (using a listbox to select) or in some way replace that pack with what the user types in instead when i hit the delete or change button. The problem is i dont know how to get the "new" text to the same position as the old when i change it. Anyone got an idea? Sorry if i confuse you :) --

      Light is faster than sound... Thats why some people look smart till they start to talk.

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      How about:

      TextReader tr = new StreamReader("test.txt");
      int numberOfLines = 20;
      string[] listLines = new string[NumberOfLines];

      int i=0;
      while(i < numberOfLines)
      {
      string line = tr.ReadLine();
      if (line == null)
      break; // End of file
      if (line == string.Empty)
      continue; // Blank line
      listLines[i] = line;
      i++;
      }


      Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

      1 Reply Last reply
      0
      • A Andrew Em

        Hi I got a problem with the project i'm building, i'm using StreamWriter and to append text to a textfile, so the text file look like this: entry1 entry2 entry3 *blank row* entry4 entry5 entry6 *blank row* i do that with this code: StreamWriter sw = File.AppendText("test.txt"); sw.WriteLine(tb1.Text); sw.WriteLine(tb2.Text + " : " + tb21.Text); sw.WriteLine(tb3.Text); sw.WriteLine(); sw.Flush(); sw.Close(); Then i use a loop and array to read those files into my application. TextReader TR = new StreamReader("test.txt"); int NumberOfLines = 20; string[] ListLines = new string[NumberOfLines]; for (int i = 1; i < NumberOfLines; i++) { ListLines[i] = TR.ReadLine(); } Now i got the problem, i need to be able to delete/change the "packs" between the blank line, was thinking something like making a backup of the file and re-writing it, without the selected pack (using a listbox to select) or in some way replace that pack with what the user types in instead when i hit the delete or change button. The problem is i dont know how to get the "new" text to the same position as the old when i change it. Anyone got an idea? Sorry if i confuse you :) --

        Light is faster than sound... Thats why some people look smart till they start to talk.

        B Offline
        B Offline
        bobsugar222
        wrote on last edited by
        #3

        Is there any reason why you have to work with text files? If not I would do something like this: (There's a bug in the pre tag... it doesn't like generics in examples!) using System; using System.Collections.Generic; using System.IO; using System.Xml.Serialization; [Serializable()] [XmlRootAttribute("Pack")] public class Pack { private List items; public Pack() { items = new List(); } public List Items { get { return items; } set { items = value; } } public void Load(string filename) { XmlSerializer serialiser = new XmlSerializer(typeof(Pack)); Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read); this.Items = ((Pack)serialiser.Deserialize(stream)).Items; stream.Close(); } public void Save(string filename) { XmlSerializer serialiser = new XmlSerializer(typeof(Pack)); Stream stream = new FileStream(filename, FileMode.Create, FileAccess.Write); serialiser.Serialize(stream, this); stream.Close(); } }

        G A 2 Replies Last reply
        0
        • B bobsugar222

          Is there any reason why you have to work with text files? If not I would do something like this: (There's a bug in the pre tag... it doesn't like generics in examples!) using System; using System.Collections.Generic; using System.IO; using System.Xml.Serialization; [Serializable()] [XmlRootAttribute("Pack")] public class Pack { private List items; public Pack() { items = new List(); } public List Items { get { return items; } set { items = value; } } public void Load(string filename) { XmlSerializer serialiser = new XmlSerializer(typeof(Pack)); Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read); this.Items = ((Pack)serialiser.Deserialize(stream)).Items; stream.Close(); } public void Save(string filename) { XmlSerializer serialiser = new XmlSerializer(typeof(Pack)); Stream stream = new FileStream(filename, FileMode.Create, FileAccess.Write); serialiser.Serialize(stream, this); stream.Close(); } }

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          bobsugar222 wrote:

          (There's a bug in the pre tag... it doesn't like generics in examples!)

          That's because it doesn't html encode the text, so the browser thinks that it is a broken html tag and ignores it. You can use < and > to display the < and > characters.

          --- Year happy = new Year(2007);

          B 1 Reply Last reply
          0
          • G Guffa

            bobsugar222 wrote:

            (There's a bug in the pre tag... it doesn't like generics in examples!)

            That's because it doesn't html encode the text, so the browser thinks that it is a broken html tag and ignores it. You can use < and > to display the < and > characters.

            --- Year happy = new Year(2007);

            B Offline
            B Offline
            bobsugar222
            wrote on last edited by
            #5

            Sorted. Cheers :)

            1 Reply Last reply
            0
            • B bobsugar222

              Is there any reason why you have to work with text files? If not I would do something like this: (There's a bug in the pre tag... it doesn't like generics in examples!) using System; using System.Collections.Generic; using System.IO; using System.Xml.Serialization; [Serializable()] [XmlRootAttribute("Pack")] public class Pack { private List items; public Pack() { items = new List(); } public List Items { get { return items; } set { items = value; } } public void Load(string filename) { XmlSerializer serialiser = new XmlSerializer(typeof(Pack)); Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read); this.Items = ((Pack)serialiser.Deserialize(stream)).Items; stream.Close(); } public void Save(string filename) { XmlSerializer serialiser = new XmlSerializer(typeof(Pack)); Stream stream = new FileStream(filename, FileMode.Create, FileAccess.Write); serialiser.Serialize(stream, this); stream.Close(); } }

              A Offline
              A Offline
              Andrew Em
              wrote on last edited by
              #6

              Thank you for your answers. The reason i work with text files is that this is my first "real" c# project, and i'm not very skilled in using it. Was mainly playing around trying to find a way to solve my problems. Found a number of problems with it though so thinking of using access database instead. If you could add some comments to the above code example i'd be grateful, dont really understand every part of it. Thanks --

              Light is faster than sound... Thats why some people look smart till they start to talk.

              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