5 Replies - 4046 Views - Last Post: 11 September 2009 - 03:17 AM Rate Topic: -----

#1 laailalalaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-September 09

append text at end of line

Posted 11 September 2009 - 01:03 AM

hello:),

i'm using c# and i want to read a file line by line, and, if some condition occurs, append a string at the end of the read line. how can this be done? with StreamReader the cursor gets to the next line.

thanks
Is This A Good Question/Topic? 0
  • +

Replies To: append text at end of line

#2 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: append text at end of line

Posted 11 September 2009 - 01:57 AM

hopefully something like this will get you on the right track:
		private void ReadLineByLine()
		{
			StreamReader sr = null;

			try
			{
				sr = new StreamReader(@"C:\ally.txt");
				string line = sr.ReadLine();

				while (line != null)
				{
					//if (condition)
					//{
					//  line += whatever...
					//}

					// read next line
					line = sr.ReadLine();
				}
				
			}
			catch (IOException exc)
			{
				MessageBox.Show(exc.Message);
			}
			finally
			{
				if (sr != null)
				{
					sr.Close();
				}
			}
		}


Was This Post Helpful? 0
  • +
  • -

#3 laailalalaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-September 09

Re: append text at end of line

Posted 11 September 2009 - 02:04 AM

thanks, batesyk3k, for your reply, but your code will change lines taken out of the file. i need the changes to be made on the actual file.
Was This Post Helpful? 0
  • +
  • -

#4 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: append text at end of line

Posted 11 September 2009 - 02:30 AM

So you mean you want to append some lines to the end of the file?
Because the way you asked your question it seemed you wanted to change some line, which is what batesyk3k did.
Was This Post Helpful? 0
  • +
  • -

#5 laailalalaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-September 09

Re: append text at end of line

Posted 11 September 2009 - 02:42 AM

sorry i wasn't clear enough the 1st time. no, i want to append text at the end of some lines in a simple .txt file. and i don't want to read all the lines in an array of strings, make the changes and rewrite the file.
thanks
Was This Post Helpful? 0
  • +
  • -

#6 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: append text at end of line

Posted 11 September 2009 - 03:17 AM

I'd like to know why you don't want to read all the lines in an array of string first and rewrite the file. That's the easiest solution.

But as you wish, here is the code which uses filestream and edits each line when the condition is met. Only one flaw: it won't edit the last line but I'll leave that correction to you as I have to get going now and don't have time to check that:

			string path = @"C:\temp\my.txt";

			FileStream fileStream = new FileStream(path, FileMode.Open);

			int b;

			while ((b = fileStream.ReadByte()) > 0)
			{
				char c = Convert.ToChar(b);
				if (c == '\r')
				{
					// end of the line reached
					if (true)
					{
						string myText = "my text";
						myText += Environment.NewLine;
						byte[] ba = Encoding.ASCII.GetBytes(myText);
						long pos = fileStream.Position;
						byte[] bar = new byte[fileStream.Length - pos];
						fileStream.Read(bar, 0, bar.Length);

						fileStream.SetLength(fileStream.Length + ba.Length);

						fileStream.Position = pos;
						fileStream.Write(ba, 0, ba.Length);
						fileStream.Write(bar, 0, bar.Length);
						fileStream.Position = pos + ba.Length;
					}
				}
			}

			fileStream.Close();


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1