Jump to content
  • Advertisement
Sign in to follow this  
TheScriptan

How to read file line

This topic is 2065 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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. :(

Share this post


Link to post
Share on other sites
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... Edited by brx

Share this post


Link to post
Share on other sites
while (File.Exists("Settings.dat"))
        {
            options.username = reader.ReadLine();

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

What is wrong in here?

Edited by TheScriptan

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!