Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System; // This is the core namespace, this let's us do stuff with strings, and write to the Console.
- using System.IO; // This let's us interact with IO (Input/Output), aka File IO.
- using System.Collections.Generic; // This let's us create Dictionaries (collections), and work with them.
- /*
- ---
- * PURPOSE OF THIS PROGRAM *
- This program was created for educational purposes.
- This program reads a file line-by-line given in by a user.
- For each line, it splits it by the equals sign, and stores the value into a key-value pair.
- For example, if a line was "apple=good fruit" then it would store it with the key being "apple" and value being "good fruit."
- Once it is done reading the file, a user can request the value of a key by giving the application input.
- If the value exists, it'll give it, if it doesn't, it'll notify the user.
- This program is commented in detail to explain how it works. Have fun learning!
- This program was created by bin to teach pixel core C# functions.
- ---
- */
- namespace MyApp // Our namespace is decalred
- {
- public class Program // The program's namespace is declared.
- {
- // These are the variables of the program, used globally inside it.
- static string file_name; //This is the filename that we will be reading
- static Dictionary<string, string> values = new Dictionary<string, string>();
- /*
- This is a Dictionary. Dictionary is a special type of List with a key-value pair
- For example, you can have the key "apples" link to "good fruit." Dictionaries are an extremely useful class
- used throughout C# coding for all kinds of coding, from tasks, to storing quick data, to this !
- Have fun playing with Dictionaries !
- */
- public static void Main(string[] args) // The entry point of the program, where all the stuff happens.
- {
- Console.WriteLine("Enter the file name you would like to read:"); //These next couple lines of code are self explanatory.
- Console.Write("> ");
- file_name = Console.ReadLine(); // We take the file name as an input from the Console, and store it in the file_name variable.
- if(string.IsNullOrWhiteSpace(file_name)) // Now we check if it's null or just spaces, and if so, we'll change it back to the default file name
- file_name = "items.txt"; // which is done here.
- if(!File.Exists(file_name)) // Now we check if the file exists, if it doesn't we create a new file with the same name as file_name
- {
- File.Create(file_name).Close(); // Here as you can see, the file is created, and then the FileStream returned by .Create() is closed.
- Console.WriteLine("Error! File did not exist and has been created!"); //We notify the user
- return; //Then we use the "meh" method to exit the program (atleast only usable in this situation, in the main program).
- //Up here we are using the *meh* method to exit the program.
- }
- string[] file_data = File.ReadAllLines(file_name); // We read the file by **lines** and store it an array, each value of the array is a different line
- int line_number = 1; // We take a note of the line number, it starts from 1 in this case for ease of use, but can be 0 as well.
- foreach(string data in file_data) // This is a foreach loop, it executes that block of code for each value in the array, and assigns it to a temp variable called "data"
- {
- if(!string.IsNullOrWhiteSpace(data)) // We check if the data is null/empty or not.
- {
- string[] key_value = data.Split('='); //string.Split(data, '=');
- //Here we split the data by the equals sign.
- /* Side note:
- This program does not account for a second equals sign, it will just ignore it and not take it in as an input.
- */
- //Here we check to make sure that there are actually two values, one key, and one value for the key.
- if(key_value.Length >= 2)
- {
- if(string.IsNullOrWhiteSpace(key_value[0])) // Now we check if the key is blank or not, if it is, it's invalid.
- Console.WriteLine("Invalid data key on line " + line_number); // Notify the user.
- else if(string.IsNullOrWhiteSpace(key_value[1])) // Now we check if the data value for the key is blank, if it is, it's invalid
- Console.WriteLine("Invalid data value on line " + line_number); // Notify the user.
- else // If it's not invalid, this gets called.
- values.Add(key_value[0], key_value[1]); // and we add the key-value pair to the dictionary.
- }
- else // If there aren't two values, or something blank, or some other shit, we notify the user
- Console.WriteLine("Invalid data provided on line " + line_number); // that the data is invalid.
- }
- else
- Console.WriteLine("Invalid data provided on line " + line_number); // We should do it here, just incase if it's null or white-spaced.
- line_number++; // Here we make the line number go up by one so we have a refrence to what line we are on, to notify the user.
- // Then the loop iterates until it has gone through every value in the array.
- }
- Console.WriteLine("Finished reading file '" + file_name + "'"); // We notify the user that it's done.
- Console.WriteLine("Enter a value, or a command. Use -h for commands or -e to exit.\n"); // We notify the user of the possible commands, and etc.
- while(true) // Now we go into a forever while loop, this goes on forever till the program exits or is killed.
- {
- Console.Write("> "); // We write a cool command-hackery-looking thing down here.
- string input = Console.ReadLine(); // We read the input
- if(!string.IsNullOrWhiteSpace(input)) // We check to make sure that it's not null or spaces, and if it is, we just keep looping.
- {
- input = input.Trim(); // We trim the input from extra spaces and etc.0
- string cmd = input.ToLower(); // We create a lowered version of the input to check for commands (just incase if user types -E instead of -e).
- if(cmd == "-h") // If it's the help command, just notify the user with a list of commands.
- Console.WriteLine("Commands:\n-h : Lists commands.\n-v : Lists all values\n-e : Exits ths program.");
- else if(cmd == "-e") // If it's the exit command:
- {
- Console.WriteLine("Now exiting..."); // Notify the user we are exiting
- // There are two ways to exit the program in this situation:
- // We can exit the normal way, OR we can just end the Main() function and boom.
- // For science, we will do it the proper way.
- Environment.Exit(0); // Exit the program with exit code 0, which means execution succesfull.
- // The unproper way:
- // return;
- }
- else if(cmd == "-v") // if it's the list all values command then we list all the values.
- {
- string output_text = "All values:\n"; // We create a template variable, start it with same basic values:
- foreach(var pair in values) // Iterate through all the keys
- {
- output_text += $"'{pair.Key}'\n"; // Add '{KEY_NAME}' to the string above
- // The += means:
- // output_text = output_text + new_value;
- }
- Console.WriteLine(output_text); // We output the text
- }
- else // If it's not a command, we'll take the input and check if it's a key in our memory.
- {
- if(values.ContainsKey(input)) // If it is
- Console.WriteLine($"Value for '{input}' is: '{values[input]}'"); // We'll just output it here
- else
- Console.WriteLine($"Value not found for '{input}'"); // If it isn't then we'll notify the user
- }
- // We don't do anything, the while loop iterates forever until the program exits, or is killed.
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement