Skip to content

Instantly share code, notes, and snippets.

@fiskefyren
Created March 5, 2016 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fiskefyren/cfb9325b2fac3f46c9e5 to your computer and use it in GitHub Desktop.
Save fiskefyren/cfb9325b2fac3f46c9e5 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
//list is a class object that #
//can be used for put dynamic data into
List<int> intList = new List<int>();
List<float> floatList = new List<float>();
List<char> charList = new List<char>();
List<bool> boolList = new List<bool>();
List<string> stringList = new List<string>();
//StreamReader is a class #
System.IO.StreamReader txtFile = new System.IO.StreamReader("data.txt");
string line; //contains for each line through the loop
//line is a text-string / char-array
while ((line = txtFile.ReadLine()) != null) { //reads every line in the txt file until it reaches the end of file
//Console.WriteLine((int)line[0]); //just there to test if the txt file worked
//decimal 48-57, which will be 0-9 (Symbol)
bool isInt = true; //is set to true because
bool isFloat = true;
//we use char because char is pretty much already an ascii
foreach (char c in line) { //reads through every char for each text-string
int ascii = (int) c; //casts a char into its ascii representation in int
if(ascii < 48 || ascii > 57) { //if any char is outside 48-57 = 0-9, then its not an int
isInt = false; //so any char that is not between 48-58 is therefore not an int (int = false)
}
if((ascii < 48 || ascii > 57) && ascii != 46) { //dot '.' = 46, but 46 is smaller than 48, ...
isFloat = false; //so any char that is not between 48-58 and is not 46 is therefor not a float (flaot = false)
}
}
if (line.Length < 1) { //we use "<1" because if the line is empty, which is less than 1
Console.WriteLine("Empty line here");
} else if (isInt) {
//Parse #
int result = Int32.Parse(line); //we use Int32.Parse to change line (string) into an int
intList.Add(result); //adds the result to the intList of varibles
Console.WriteLine("it's an int = " + result);
} else if (isFloat) {
float result = float.Parse(line);
floatList.Add(result);
Console.WriteLine("it's a float = " + result);
} else if (line == "true" || line == "false") {
bool result = Boolean.Parse(line);
boolList.Add(result);
Console.WriteLine("it's a bool = " + result);
} else if (line.Length == 1) { //==1 because a char is just a single symbol, e.g. 'a','b','c'
char result = line[0]; //takes the first char of the list/array and because there can only be one highlander!
charList.Add(result);
Console.WriteLine("it's a char = " + result);
} else if (line.Length > 1) { //>1 because a string is multiple symbols, e.g. "grimme mand"
stringList.Add(line);
Console.WriteLine("it's a string = " + line);
}
}//while-loop ends
txtFile.Close(); //whenever you open a file it only make sense to close it afterwards
//e.g. if more programs are using the same file, then they might overwrite each other and make a mess
//or if we don't do that then the OS won't be allowed access to the file, or make an error or might goes on a queue
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment