Text file handling issue
-
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.
-
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.
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
-
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.
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(); } }
-
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(); } }
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);
-
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);
Sorted. Cheers :)
-
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(); } }
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.