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

This topic is 4706 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 on other sites
Quote:
 Original post by TangentZYou 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 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 on other sites
Quote:
 Original post by MaulingMonkeyusing (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.

• 23
• 10
• 19
• 16
• 14