简体   繁体   中英

C# retrieve different Data Types data from a text file and store it to variables

So what problem have i ran into - im trying to create a complex(complex for me) Bank management system. It takes various data from a person. It takes Strings and ints. I managed to store that data to a text file.

Text File looks Like this:

AccID: 74016 | NAME: anyname | AGE: 18 | LASTNAME: anylastname | BALANCE: 300 | ACCOUNT TYPE: Savings Account |

(this data is in one row in a text file)

this is how i store it:

              var StoreInFile = new StreamWriter(@"C:\Accounts.txt", true);


                StoreInFile.Write("AccountID: " + AccID + " | ");
                StoreInFile.Write("NAME: " + _Name + " | ");
                StoreInFile.Write("AGE: " + _Age + " | ");
                StoreInFile.Write("LASTNAME: " + _LastName + " | ");
                StoreInFile.Write("BALANCE: " + Balance + " | ");
                StoreInFile.Write("ACCOUNT TYPE: " + accountType + " | ");
                StoreInFile.WriteLine(" ");
                StoreInFile.WriteLine(" ");

                StoreInFile.Close();

The problem now is how do i get that data from it? how do i store all those different strings/ints back to variables, where then i can manipulate them?

Ideally, i'd love to only input AccountID when logging in, and then it would automatically store all that Name, LastName, Age... and so on Data into variables back in the code, which are in that row. Hope my question is clear.

var properties = new prop();
var json = JsonConvert.SeralizeObject(properties);

public class prop 
{
     public string account_id { get; set;}
     public string name { get; set;}
     public int age { get; set;}

}

Something like this would help you a ton.

You can create a account class with properties you used:

class Account
{
    public int Id { get; set;}
    public string Name { get; set;}
    public int Age{ get; set;}
    public string Lastname { get; set;}
    public int Balance { get; set;}
    public string AccountType { get; set;} // this is better to be enum but your example shows you have stored string
}

Then you can create an instance of Account class read the file split it with | and just set its properties (parse to int where needed):

string[] data = File.ReadAllText("filepath.txt").Split('|');
Account account = new Account();
foreach (string item in data)
{
    string[] sitem = item.Split(':');
    switch(sitem[0].Trim())
    {
        case "AccountID":
           account.Id = int.Parse(sitem[1]); break;
        case "Name":
           account.Name = int.Parse(sitem[1]); break;  
           ....
    }
}

Actualy i advice you that you sould keep it in Json/XML format. But maybe BinaryFormatter helps your case here the some examples that you can use;

class Program {
    static void Main (string[] args) {
        // Create Object
        Account account = new Account {

            Id = 100,
            Name = "Berkay"
        };

        // You can use BinaryFormatter 
        IFormatter formatter = new BinaryFormatter ();
        Stream stream = new FileStream ("D:\\account.txt", FileMode.Create, FileAccess.Write);

        formatter.Serialize (stream, account);
        stream.Close ();

        stream = new FileStream ("D:\\account.txt", FileMode.Open, FileAccess.Read);
        Account readAccount = formatter.Deserialize(stream) as Account;

        System.Console.WriteLine(readAccount.Id);
        System.Console.WriteLine(readAccount.Name);

        Console.ReadLine();
    }

}

    // Serializable attribute
    [Serializable]
    public class Account {
    public int Id { get; set; }
    public string Name { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM