Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using StreamReader.ReadLine in loop only reads one line from text file

see comments for resolution--file was in wrong place

I've searched for an answer all over the place, but I haven't been able to find one. This is really frustrating to me because I've never had this much trouble reading from a file with any other programming language.

I'm trying to extract usernames and passwords from a text file for a basic instant messaging program. I'm not going to post all the code--it's too long, and it more than likely isn't relevant since the text file is being read at the very beginning of the program.

Here's the contents of the text file ("users.ul") I'm trying to read from:

admin.password
billy.bob
sally.sal

Here is the code that reads from the text file:

users = new Dictionary<string, User>();

System.Console.WriteLine("users.ul exists: " + File.Exists("users.ul"));

// Check the status of users.ul. If it exists, fill the user dictionary with its data.
if (File.Exists("users.ul"))
{
    // Usernames are listed first in users.ul, and are followed by a period and then the password associated with that username.
    StreamReader reader = new StreamReader("users.ul");
    string line;
    int count = 0;

    while ((line = reader.ReadLine()) != null)
    {
        string[] splitted = line.Split('.');
        string un = splitted[0].Trim();
        string pass = splitted[1].Trim();

        User u = new User(un, pass);

        // Add the username and User object to the dictionary
        users.Add(un, u);

        count++;
    }

    System.Console.WriteLine("count: " + count);

    reader.Close();
}

This is the output my code produces:

users.ul exists: True
count: 1

The only data added to the users dictionary is "admin" with the password "password". The other lines are ignored.

Please help me out here. My program is useless without multiple users. I've looked everywhere for a solution, including the other similar questions on this site. Never thought that reading from a file would cause me to waste so much time.

like image 779
Steve Schmith Avatar asked Dec 07 '22 08:12

Steve Schmith


1 Answers

Unless you have a specific need to go through the rigmarole of using StreamReader, I suggest using File.ReadAllLines(), which returns an (enumerable) string array.

Better yet, use linq :-)

System.Console.WriteLine("users.ul exists: " + File.Exists("users.ul"));

// Check the status of users.ul. If it exists, fill the user dictionary with its data.
if (File.Exists("users.ul")) {
    var lines = File.ReadAllLines("users.ul");
    // Usernames are listed first in users.ul, and are followed by a period
    // and then the password associated with that username.
    var users = lines.Select(o => o.Split('.'))
                     .Where(o => o.Length == 2)
                     .Select(o => new User(o[0].Trim(), o[1].Trim());

    System.Console.WriteLine("count: " + users.Count());
}
like image 73
theMayer Avatar answered May 13 '23 21:05

theMayer