Writing a specific line to a txt file

Done tutorial on reading a specific line but also want to write it

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 9012 Views - Last Post: 01 January 2009 - 01:50 PM Rate Topic: -----

#1 Mania-92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 29-December 08

Writing a specific line to a txt file

Posted 29 December 2008 - 01:46 PM

Hi, I done the tutorial on this site on reading a specific line from a file. This works great but now I want to write a specific line to a file, for example:

file.txt said:

Hello 87878
Shoelaces 8349
Pancake 235

I want to write te line "Shoelaces 9934" to the file and replace the old Shoelaces line with this new one. How do I do that?
Is This A Good Question/Topic? 0
  • +

Replies To: Writing a specific line to a txt file

#2 Xerelin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-December 08

Re: Writing a specific line to a txt file

Posted 29 December 2008 - 03:12 PM

View PostMania-92, on 29 Dec, 2008 - 12:46 PM, said:

Hi, I done the tutorial on this site on reading a specific line from a file. This works great but now I want to write a specific line to a file, for example:

file.txt said:

Hello 87878
Shoelaces 8349
Pancake 235

I want to write te line "Shoelaces 9934" to the file and replace the old Shoelaces line with this new one. How do I do that?


You can use the streamreader and streamwriter functions for this.

using (Streamreader read_file = new streamreader("txt_file.txt");
		  {
			   string search_for_Line = read_file.ReadLine();

			   if (search_for_line.Contains("Shoelaces")
			   {
					using (Streamwriter write_file = new streamwriter("txt_file.txt");

				   {
						string shoelace = "Shoelaces " + shoelacevariable.ToString();
						write_file.WriteLine(shoelace);
				   }
				}
		   }


this should write to the text file specified if you input your variable names in the place of the "fakey" vars I included.

Hope this helps,

Xer
Was This Post Helpful? 0
  • +
  • -

#3 Mania-92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 29-December 08

Re: Writing a specific line to a txt file

Posted 29 December 2008 - 03:53 PM

Copied your code quite literally to test it but the compiler says it doesn't know a lot of variables/objects. And as far as my knowledge of C# reaches, they aren't out of the scope of where they are declared (if that's formulated correctly?).

public void WriteSpecifiedLine(string file)
		{
		  using (StreamReader stream = new StreamReader(file));
		  {
			  string search_for_Line = [u]stream[/u].ReadLine();
			  
			   if ([u]search_for_line[/u].Contains("test"))
			   {
					using (StreamWriter write_file = new StreamWriter(file));

				   {
						string shoelace = "test";
						[u]write_file[/u].WriteLine(shoelace);
				   }
				}
		  }
		}

The one with underscores are the ones the compiler is complaining about..
[EDIT]Well, atleast where the [u] BB-codes are.. :P

This post has been edited by Mania-92: 29 December 2008 - 03:54 PM

Was This Post Helpful? 0
  • +
  • -

#4 Xerelin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-December 08

Re: Writing a specific line to a txt file

Posted 29 December 2008 - 03:54 PM

Just as another thought... there's a way to refresh your entire file all at once if you need it. This will overwrite the old values and input new ones.

using (Streamwriter write_file = new streamwriter("txt_file.txt");

				   {
						write_file.Clear();

						string hello = "Hello " + hellovariable.ToString();
						string shoelace = "Shoelaces " + shoelacevariable.ToString();
						string pancake = "Pancake " + pancakevariable.ToString();
						write_file.WriteLine(hello);
						write_file.WriteLine(shoelace);
						write_file.WriteLine(pancake);
				   }


if you stick that into an I/O section of your code then you can call it to refresh your file's data.

Whereas if you want to read the file's data just use the streamreader version of the same function.

Let me know if this works,

Xer

The variables with underscores? which ones exactly?
Was This Post Helpful? 0
  • +
  • -

#5 Mania-92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 29-December 08

Re: Writing a specific line to a txt file

Posted 29 December 2008 - 03:55 PM

View PostXerelin, on 29 Dec, 2008 - 02:54 PM, said:

Just as another thought... there's a way to refresh your entire file all at once if you need it. This will overwrite the old values and input new ones.

using (Streamwriter write_file = new streamwriter("txt_file.txt");

				   {
						write_file.Clear();

						string hello = "Hello " + hellovariable.ToString();
						string shoelace = "Shoelaces " + shoelacevariable.ToString();
						string pancake = "Pancake " + pancakevariable.ToString();
						write_file.WriteLine(hello);
						write_file.WriteLine(shoelace);
						write_file.WriteLine(pancake);
				   }


if you stick that into an I/O section of your code then you can call it to refresh your file's data.

Whereas if you want to read the file's data just use the streamreader version of the same function.

Let me know if this works,

Xer

The variables with underscores? which ones exactly?

Yeah but with big files that will cost a lot of lines doesn't it, btw check my post above yours..

The ones between the [u] [/ u] BB-codes, forget [code] doesn't handle BB-codes so they aren't underscored.

This post has been edited by Mania-92: 29 December 2008 - 03:57 PM

Was This Post Helpful? 0
  • +
  • -

#6 Xerelin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-December 08

Re: Writing a specific line to a txt file

Posted 29 December 2008 - 03:59 PM

Are you using the assembly that stramreader/streamwriter is contained in? that could be the problem

you can lookup the assembly reference in MSDN library. I don't know it off the top of my head, but it's fairly common.
Was This Post Helpful? 0
  • +
  • -

#7 Mania-92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 29-December 08

Re: Writing a specific line to a txt file

Posted 29 December 2008 - 03:59 PM

View PostXerelin, on 29 Dec, 2008 - 02:57 PM, said:

Are you using the assembly that stramreader/streamwriter is contained in? that could be the problem

What do you mean by using it?

Btw maybe this image makes it clearer:

[EDIT] Ah with re-reading your post is makes sense, yes I'm using the correct assembly: System.IO

Attached image(s)

  • Attached Image

This post has been edited by Mania-92: 29 December 2008 - 04:01 PM

Was This Post Helpful? 0
  • +
  • -

#8 Xerelin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-December 08

Re: Writing a specific line to a txt file

Posted 29 December 2008 - 04:04 PM

can you give me a screen cap of the very top of your code (where it declares your assembly references)?

ok, if you have the right assembly then the problem lies with the program doesn't think the variable "stream" is defined. this could have to do with the file path.

The var "file" needs to be a correct filepath to the file AND must be saved as a string.
Was This Post Helpful? 0
  • +
  • -

#9 Mania-92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 29-December 08

Re: Writing a specific line to a txt file

Posted 29 December 2008 - 04:04 PM

Already found the problem, you used a semicolon after the using "thing", it shouldn't be there, so problem solved..
Thanx.
Was This Post Helpful? 0
  • +
  • -

#10 Xerelin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-December 08

Re: Writing a specific line to a txt file

Posted 29 December 2008 - 04:06 PM

oh, my bad. I should've seen that. I HATE Syntax errors. but I don't get them very often. glad I could help.
Was This Post Helpful? 0
  • +
  • -

#11 Mania-92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 29-December 08

Re: Writing a specific line to a txt file

Posted 29 December 2008 - 04:22 PM

Though it compiled it crashes on runtime, this is my code to test:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			WriteSpecifiedLine("test.txt");

			Console.ReadKey();
		}

		public static void WriteSpecifiedLine(string file)
		{
			using (StreamReader stream = new StreamReader(file))
			{
				string search_for_Line = stream.ReadLine();

				if (search_for_Line.Contains("test123"))
				{
					using (StreamWriter write_file = new StreamWriter(file))
					{
						write_file.WriteLine("hay");
					}
				}
			}
		}
	}
}



And this is my file:

test.txt said:

test123


And I get the following error:
Attached Image
Free translation: The program doesn't work anymore
There is being searched for a solution for the problem

The crash comes from this part of code, because if I comment it, it doesn't crash:
using (StreamWriter write_file = new StreamWriter(file))
					{
						write_file.WriteLine("hay");
					}

This post has been edited by Mania-92: 29 December 2008 - 04:24 PM

Was This Post Helpful? 0
  • +
  • -

#12 Xerelin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-December 08

Re: Writing a specific line to a txt file

Posted 29 December 2008 - 05:24 PM

It's a file access problem, I'm writing up the solution now
Was This Post Helpful? 0
  • +
  • -

#13 Xerelin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-December 08

Re: Writing a specific line to a txt file

Posted 29 December 2008 - 05:50 PM

I got it fixed. as I said before it was an access error. So I closed the streamreader before I opened the streamwriter and even put the minto separate functions just in case.

here's the code: Let me know if it doesn't work

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			ReadSpecifiedLine("test.txt");

			Console.ReadKey();
		}

		public static void ReadSpecifiedLine(string file)
		{
			using (StreamReader stream = new StreamReader(file))
			{
				string search_for_Line;

				do
				{
					search_for_Line = stream.ReadLine();

					stream.Close();
					WriteSpecifiedLine(search_for_Line, file);
				}
				while (search_for_Line.Equals("test123"));
			}
		}
		public static void WriteSpecifiedLine(string search_for_Line,string file)
		{
			if (search_for_Line.Contains("test123"))
			{
				using (StreamWriter write_file = new StreamWriter(file))
				{
					write_file.WriteLine("hay");
					write_file.Close();
				}
			}
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#14 Mania-92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 29-December 08

Re: Writing a specific line to a txt file

Posted 30 December 2008 - 09:25 AM

View PostXerelin, on 29 Dec, 2008 - 04:50 PM, said:

I got it fixed. as I said before it was an access error. So I closed the streamreader before I opened the streamwriter and even put the minto separate functions just in case.

here's the code: Let me know if it doesn't work

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			ReadSpecifiedLine("test.txt");

			Console.ReadKey();
		}

		public static void ReadSpecifiedLine(string file)
		{
			using (StreamReader stream = new StreamReader(file))
			{
				string search_for_Line;

				do
				{
					search_for_Line = stream.ReadLine();

					stream.Close();
					WriteSpecifiedLine(search_for_Line, file);
				}
				while (search_for_Line.Equals("test123"));
			}
		}
		public static void WriteSpecifiedLine(string search_for_Line,string file)
		{
			if (search_for_Line.Contains("test123"))
			{
				using (StreamWriter write_file = new StreamWriter(file))
				{
					write_file.WriteLine("hay");
					write_file.Close();
				}
			}
		}
	}
}


Problem with this code is it deletes the entire file and writes (in this case) hay. I want it to just delete the line that says test123 and write hay to that line, but keep all the other lines intact.

This post has been edited by Mania-92: 30 December 2008 - 09:26 AM

Was This Post Helpful? 0
  • +
  • -

#15 Xerelin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-December 08

Re: Writing a specific line to a txt file

Posted 30 December 2008 - 10:20 AM

in that case just keep them both inside the same function, but don't nest them. That way it won't delete (overwrite) the file by calling it separately.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2