How to read file line

Started by
6 comments, last by Inuyashakagome16 11 years, 3 months ago

How can I read file line. For example I create file settings.dat and i want to store two lines in it, color id, and username. How do i need to do. I tried to google but it was not worth it. :(

Check out my blog or twitter for news how I am doing in the project "Learn Programming in 2 Years!". You can follow my progress, and help me! :)

My Projects:

"Learn Programming In 2 Years"

"Windows Tutorials"

"Ubuntu Tutorials"

My Games:

Moving@ IndieDB: http://www.indiedb.com/games/moving-rl

My Links:

My Blog: http://www.thescriptan.blogspot.com

My Twitter: https://twitter.com/TheScriptan

Advertisement
Judging by the question, you do not only want to read a line from a file, but also write it, right?

Writing:

TextWriter writer = new StreamWriter("settings.dat");
writer.WriteLine(colorId);
writer.WriteLine(userName);
writer.Close();
Reading:

TextReader reader = new StreamReader("settings.dat");
String colorId = reader.ReadLine();
String userName = reader.ReadLine();
reader.Close();
Note that you also should do some error checking, right now bad things will happen if, for example the file cannot be opened or the file does not have two lines in it...

So first string will read first line, and the second string will read second line. Am i right?

Check out my blog or twitter for news how I am doing in the project "Learn Programming in 2 Years!". You can follow my progress, and help me! :)

My Projects:

"Learn Programming In 2 Years"

"Windows Tutorials"

"Ubuntu Tutorials"

My Games:

Moving@ IndieDB: http://www.indiedb.com/games/moving-rl

My Links:

My Blog: http://www.thescriptan.blogspot.com

My Twitter: https://twitter.com/TheScriptan

Embedded statement cannot be a declaration or labeled statement. what to do?

Check out my blog or twitter for news how I am doing in the project "Learn Programming in 2 Years!". You can follow my progress, and help me! :)

My Projects:

"Learn Programming In 2 Years"

"Windows Tutorials"

"Ubuntu Tutorials"

My Games:

Moving@ IndieDB: http://www.indiedb.com/games/moving-rl

My Links:

My Blog: http://www.thescriptan.blogspot.com

My Twitter: https://twitter.com/TheScriptan

Maybe this is the problem

http://forums.asp.net/t/1239769.aspx/1


while (File.Exists("Settings.dat"))
        {
            options.username = reader.ReadLine();

            Console.WriteLine("User: " + options.username);
            reader.Close();
        }

What is wrong in here?

Check out my blog or twitter for news how I am doing in the project "Learn Programming in 2 Years!". You can follow my progress, and help me! :)

My Projects:

"Learn Programming In 2 Years"

"Windows Tutorials"

"Ubuntu Tutorials"

My Games:

Moving@ IndieDB: http://www.indiedb.com/games/moving-rl

My Links:

My Blog: http://www.thescriptan.blogspot.com

My Twitter: https://twitter.com/TheScriptan

Okay i finally fixed it

Check out my blog or twitter for news how I am doing in the project "Learn Programming in 2 Years!". You can follow my progress, and help me! :)

My Projects:

"Learn Programming In 2 Years"

"Windows Tutorials"

"Ubuntu Tutorials"

My Games:

Moving@ IndieDB: http://www.indiedb.com/games/moving-rl

My Links:

My Blog: http://www.thescriptan.blogspot.com

My Twitter: https://twitter.com/TheScriptan

Well what was your resolve? Like what was wrong with the code?

This topic is closed to new replies.

Advertisement