Sign in to follow this  
ChaosCommand

(C#) File Input/Output, I need help.

Recommended Posts

I'm currently making a game, Total Tennis Sim. I am programming it in C++, but I was getting a little stressed and wanted to take a break from it and I decided to get back into C#, I only learned the bare basics. Anyways, I want to read in a file, and then store information in variables. For example: I have a file that has the following: John Smith 20 English 19 15 4 16 29 Basically in my tennis game, it would be something like First Name, Last Name, Age, Nationality, Power, Strenth, Agility, Blah, Blah, blah. Anyways, I have all of this on the same line. Now is there a way that I can read in all of those into variables. In c++ it is simple as database >> fName >> lName >> age >> nationality >> blah; But how would I do this in C#. I know how to do File.ReadLine(VARIABLE); to get the whole line as a string, but what about getting numbers and names and stuff on the same line.

Share this post


Link to post
Share on other sites
You can use the Split() method of the String class.


using (StreamReader reader = File.OpenRead("MyFile.txt")) {
string line;
char[] delimiter = new char[] { ' ' };

while ((line = reader.ReadLine()) != null) {
string[] tokens = line.Split(delimiter);

firstName = tokens[0];
lastName = tokens[1];
// etc
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by TangentZ
You can use the Split() method of the String class.

*** Source Snippet Removed ***




That works perfectly... I modified it to work with my game, but I just don't understand HOW it works, I'd probably not be able to do it again unless I asked for help again.

MSDN doesn't really offer an explination in english terms *at least that is how I look at it*

Any additional insight into how it works, or maybe a step by step of what it does, it would be awesome.

Share this post


Link to post
Share on other sites
using (StreamReader reader = File.OpenRead("MyFile.txt")) {

/* Not knowing C#, I don't know exactly what "using" does (I'm guessing
* something to do with scope), but this is similar to, in C++:
* std::ifstream reader("MyFile.txt");
* Should be pretty obvious. You're creating a Input/Reader Stream which you'll
* use to read from the file "MyFile.txt"
*/


string line;

/* Again, self evident. In C++ this would be:
* std::string line;
* Or in fact the exact same thing if you were using the standard namespace.
*/


char[] delimiter = new char[] { ' ' };

/* Again, pretty obvious. Creates an array of characters called delimiter.
* If we wanted to parse a file shaped like this:
* Full Name (Can Include Spaces),Age,Weight,Etc
* We'd put a comma rather than a space as the delimiter(s).
*/


while ((line = reader.ReadLine()) != null) {

/* Yet once more, pretty obvious. Basically, read one line at a time from our
* stream, until we run out of lines. In C++ we'd write:
* while( std::getline( reader , line ) ) {
*/


string[] tokens = line.Split(delimiter);

/* This uses a C# string function, split, to seperate our line into multiple
* strings. If our line was "I like pie", and we used a space for our delimiter,
* tokens[] would contain: { "I" , "like" , "pie" }.
* This can be done similarly in C++ with a few extra lines like so:
* std::string token;
* std::vector< std::string > tokens;
* std::stringstream ss( line );
* while ( std::getline( ss , token , ' ' ) ) { //gets up to the next space
* tokens.push_back( token );
* }
*/


firstName = tokens[0];
lastName = tokens[1];


/* This one's pretty obvious again. Would work directly as is in C++ too.
*/

Share this post


Link to post
Share on other sites
Quote:
Original post by MaulingMonkey
using (StreamReader reader = File.OpenRead("MyFile.txt")) {

/* Not knowing C#, I don't know exactly what "using" does (I'm guessing
* something to do with scope), but this is similar to, in C++:
* std::ifstream reader("MyFile.txt");
* Should be pretty obvious. You're creating a Input/Reader Stream which you'll
* use to read from the file "MyFile.txt"
*/


string line;

/* Again, self evident. In C++ this would be:
* std::string line;
* Or in fact the exact same thing if you were using the standard namespace.
*/


char[] delimiter = new char[] { ' ' };

/* Again, pretty obvious. Creates an array of characters called delimiter.
* If we wanted to parse a file shaped like this:
* Full Name (Can Include Spaces),Age,Weight,Etc
* We'd put a comma rather than a space as the delimiter(s).
*/


while ((line = reader.ReadLine()) != null) {

/* Yet once more, pretty obvious. Basically, read one line at a time from our
* stream, until we run out of lines. In C++ we'd write:
* while( std::getline( reader , line ) ) {
*/


string[] tokens = line.Split(delimiter);

/* This uses a C# string function, split, to seperate our line into multiple
* strings. If our line was "I like pie", and we used a space for our delimiter,
* tokens[] would contain: { "I" , "like" , "pie" }.
* This can be done similarly in C++ with a few extra lines like so:
* std::string token;
* std::vector< std::string > tokens;
* std::stringstream ss( line );
* while ( std::getline( ss , token , ' ' ) ) { //gets up to the next space
* tokens.push_back( token );
* }
*/


firstName = tokens[0];
lastName = tokens[1];


/* This one's pretty obvious again. Would work directly as is in C++ too.
*/



Awesome, thanks for the help, I understand it perfectly now.
I wish I could learn both C# and C++, I just can't decide, that is why I'm learning both at the same time... then in the future, I can hopefully be prepared if I have to focus on one specific one.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this