-1

I wrote a code that opens my file and reads it and checks if it contains a certain username and password. but it's not working.

the method is always returning false and i don't know why.

my idea is to make my code read every line and split each line into 3 strings in an array, and then to validate if each string has the username or password. If it finds only one it goes to another line, if it finds both in the same line it breaks

        public bool readFile(string username, string password, string path)
        {
            int cntr = 0;
            string[] lines = File.ReadAllLines(path);
            foreach (string line in lines)
            {
                cntr = 0;
                string[] words = line.Split('-');
                foreach (string element in words)
                {
                    if (element == username || element == password)
                        cntr++;
                }
                if (cntr == 2)
                    break;
            }
            return cntr == 2? true : false;
        }

i tried this, and i dont know why it's not working and since im working with interfaces and diff classes im having a hard time debugging it with message boxes

EDIT: i found out by debugging it that lines is always being null, for some reason. any help?

MoeInL
  • 11
  • 3
  • 8
    This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Nov 15 '22 at 21:04
  • i would love to take this opportunity because i have seen people do it but never knew how – MoeInL Nov 15 '22 at 21:06
  • if (cntr == 2) break; should probably be on the same line – Cole Henrich Nov 15 '22 at 21:07
  • you could also have a little more code to recognize when a username has been found, because this code could return true if 2 usernames or 2 passwords are found. – Cole Henrich Nov 15 '22 at 21:08
  • Its not the cause of your problem, but you should also keep in mind the [difference between File.ReadLines() and File.ReadAllLines()](https://stackoverflow.com/questions/21969851/what-is-the-difference-between-file-readlines-and-file-readalllines). Once you figure out your core problem, knowing the differences between these might help make your code more performant. – maccettura Nov 15 '22 at 21:11
  • If you could provide the information you are parsing that may help. Perhaps you never reach a value of 2 in your counter. – Ibrennan208 Nov 15 '22 at 21:12

1 Answers1

-3
string[] words = line.Split();

will split the line by whitespace, whereas what you had looks like it splits it by a dash '-'. It probably returned false every time because line was never split into an array, so the array was never searched, so cntr never reached 2.

Cole Henrich
  • 155
  • 3
  • 17
  • 3
    We don't know the data being parsed, so this is just a guess rather than an answer. Perhaps it would make more sense as a comment? – Ibrennan208 Nov 15 '22 at 21:14
  • @lbrennan208 But you acknowledge that this would be the solution if the strings are split by spaces. And that is the most likely scenario in general. Therefore, this is likely the solution to their problem. – Cole Henrich Nov 15 '22 at 21:19
  • 3
    I didn't acknowledge that. They could have a file of blank lines when they expect it to be "username-password" pairs, using the '-' character. So your solution would actually further break their logic when they really needed their file path fixed. – Ibrennan208 Nov 15 '22 at 21:24
  • 3
    @ColeHenrich: It's equally likely that the file is delimited by pipes, or commas, or tabs, or fixed width, or values are on separate lines, or contains hidden or unexpected characters, or the OP is using the wrong file (there may be an old one from a previous test), or the data isn't present at all (perhaps the code which writes to the file failed), or this method could be throwing an exception which is caught and ignored by consuming code, etc., etc. A single random guess among countless possibilities doesn't make for a useful answer. The general advice is to "answer well-asked questions". – David Nov 15 '22 at 21:26