[C#] Why isn't this working? (File Input)

Joined
Nov 20, 2004
Messages
1,414
Reaction score
27
I've been wondering why this isn't working. I'm trying to get this to load a file, but it doesn't seem to be working. Any ideas?

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


namespace BranchesV3
{
    public class uservars
    {
        public int level;

        public void load(string name)
        {
            Stream stream = new FileStream(name + ".sfm", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
            StreamReader reader = new StreamReader(stream);

            string input = "";
            while ((input = reader.ReadLine()) != null)
            {
                level = int.Parse(input);
            }


        }

    }
}

Edit - Nevermind, I think that its working..

I think the problem is parsing the integer from the string possibly? Could anyone tell me?
 
What exactly happens? This code should work fine, can't see anything that might be a problem

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


namespace BranchesV3
{
    public class uservars
    {
        public int level;

        public void load(string name)
        {
            Stream stream = new FileStream(name + ".sfm", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
            StreamReader reader = new StreamReader(stream);

            string input = "";
            while ((input = reader.ReadLine()) != null)
            {
                level = int.Parse(input);
                Console.WriteLine( level );
            }
        }
        
        public static void Main()
        {
        	uservars u = new uservars();
        	u.load("C:/test");
        	Console.ReadKey();
        }
    }
}
I created a text file called test.sfm with 5 lines (1, 2, 3, 4, 5) and when I start the program it prints:
1
2
3
4
5

so your code seems to be correct
 
Back