How to read and write multiple lines in a file?

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 33161 Views - Last Post: 02 July 2010 - 12:50 PM Rate Topic: -----

#1 orangeinternet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 14-June 09

How to read and write multiple lines in a file?

Posted 14 June 2009 - 07:18 AM

Today I am wondering how to read and write multiple lines to a file, in windows forms. Recently, i have figured out how to write multiple lines
here is what i have discovered:

string[] lines = { "First line", "Second line", "Third line" };
			System.IO.File.WriteAllLines(@"C:\Users\Hugo\Desktop\123.txt", lines);


Now i need to read multiple lines, or just one specific line, and write one specific line too.

Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: How to read and write multiple lines in a file?

#2 RudiVisser   User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,566
  • Joined: 05-June 09

Re: How to read and write multiple lines in a file?

Posted 14 June 2009 - 07:21 AM

System.IO.File.ReadAllLines().

To write a line just..
StreamWriter sw = File.AppendText("filename");
sw.WriteLine("line of text");
sw.Close();

This post has been edited by MageUK: 14 June 2009 - 07:22 AM

Was This Post Helpful? 0
  • +
  • -

#3 orangeinternet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 14-June 09

Re: How to read and write multiple lines in a file?

Posted 14 June 2009 - 07:25 AM

View PostMageUK, on 14 Jun, 2009 - 06:21 AM, said:

System.IO.File.ReadAllLines().

To write a line just..
StreamWriter sw = File.AppendText("filename");
sw.WriteLine("line of text");
sw.Close();


Yes, but lets say i want to read the 2nd line and rewrite it?
without touching the first line?
Was This Post Helpful? 0
  • +
  • -

#4 RudiVisser   User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,566
  • Joined: 05-June 09

Re: How to read and write multiple lines in a file?

Posted 14 June 2009 - 07:27 AM

Well if you want to work with *only* File. methods then do this:
string[] Lines = File.ReadAllLines("filename.txt");
Lines[1] = "second line new value";
File.WriteAllLines("filename.txt");

Was This Post Helpful? 0
  • +
  • -

#5 orangeinternet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 14-June 09

Re: How to read and write multiple lines in a file?

Posted 14 June 2009 - 07:31 AM

View PostMageUK, on 14 Jun, 2009 - 06:27 AM, said:

Well if you want to work with *only* File. methods then do this:
string[] Lines = File.ReadAllLines("filename.txt");
Lines[1] = "second line new value";
File.WriteAllLines("filename.txt");


Are you using Windows forms, because the When it says "File" it is underlined red, do i have to define what "File" is or what?
Was This Post Helpful? 0
  • +
  • -

#6 RudiVisser   User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,566
  • Joined: 05-June 09

Re: How to read and write multiple lines in a file?

Posted 14 June 2009 - 07:33 AM

... You should take up a quick C# tutorial.

The File class is located within the System.IO namespace, but you know that already from your first post, right?
Was This Post Helpful? 0
  • +
  • -

#7 orangeinternet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 14-June 09

Re: How to read and write multiple lines in a file?

Posted 14 June 2009 - 07:36 AM

Yes, but how come your code wouldn't work?
am i missing something here or did you mess up with your code?
Was This Post Helpful? 0
  • +
  • -

#8 RudiVisser   User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,566
  • Joined: 05-June 09

Re: How to read and write multiple lines in a file?

Posted 14 June 2009 - 07:39 AM

My code works just fine... Reference the namespace.
Was This Post Helpful? 0
  • +
  • -

#9 orangeinternet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 14-June 09

Re: How to read and write multiple lines in a file?

Posted 14 June 2009 - 07:47 AM

As you can tell I'm not that good at C#, What i want is something like this:


When button1 is clicked

Newfile.txt's 3rd line = Textbox1's text

When button2 is clicked

Textbox2's text = Newfile.txt's 3rd line

Anyway, i want it to work something like that

Thanks in advance
Was This Post Helpful? 0
  • +
  • -

#10 lesPaul456   User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: How to read and write multiple lines in a file?

Posted 14 June 2009 - 08:38 AM

MageUK's code should work. Have you added "using System.IO" to the namespaces?
Was This Post Helpful? 0
  • +
  • -

#11 orangeinternet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 14-June 09

Re: How to read and write multiple lines in a file?

Posted 14 June 2009 - 09:03 AM

can you show me an example?
Was This Post Helpful? 0
  • +
  • -

#12 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: How to read and write multiple lines in a file?

Posted 14 June 2009 - 09:48 AM

just so you know, there is no built-in way for you to read a specific line in a text file or write to a specific line. You have to read in the entire file, count the lines as you loop through, then add/edit/delete when you get to the line you want.

however, if the file is a fixed length line file, then it is possible to go to a specific line.
Was This Post Helpful? 0
  • +
  • -

#13 orangeinternet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 14-June 09

Re: How to read and write multiple lines in a file?

Posted 14 June 2009 - 10:38 AM

I guess what i was really trying to accomplish was a way to say input for a textbased game, instead of making 200 files just combining them all into one
with a different meaning for each line.

If you could help that would be great!
Was This Post Helpful? 0
  • +
  • -

#14 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: How to read and write multiple lines in a file?

Posted 14 June 2009 - 02:16 PM

while I don't know much about text based games, you could easily make the file a comma-delimited file, and have the first column as some type of "id" of what the meaning of the line is. So when you read from the file, you split out that "id", then check to see what it is so the program can know how to process that line from the file.
Was This Post Helpful? 0
  • +
  • -

#15 ~Fang~   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-July 10

Re: How to read and write multiple lines in a file?

Posted 02 July 2010 - 10:31 AM

Here's an example:
(this is how my game bans characters)

public static void AddBan(string Name)
        {

            // Count the lines in the file and check 
            int counter = 0;
            string line;
            System.IO.StreamReader file = new System.IO.StreamReader("database\\banned.txt");
            while ((line = file.ReadLine()) != null)
            {
                if (line == Name)
                {
                    return;
                }
                counter++;
            }
            file.Close();

            // Add the Name 
            string[] Lines = File.ReadAllLines("database\\banned.txt");
            Lines[counter - 1] += "\n" + Name;
            File.WriteAllLines("database\\banned.txt", Lines);
        }


(It's not complete, it's just an example since I'm writing this stuff and came across the same problem)

Hope that helps!

This post has been edited by ~Fang~: 02 July 2010 - 12:31 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2