Jump to content
  • Advertisement
Sign in to follow this  
Khaos Dragon

c# text file loading

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am wondering how to replicate in c# the general functionality of the ifstream object from the c++ stl libraries in its delimination of any whitespace. For example I would like to be able to do this
while( fin >> currentWord )
{
    process word....
}


I realize that I could add in code to handle delimination myself but if it is already done I would rather do that in order to keep my code as small as possible.

Share this post


Link to post
Share on other sites
Advertisement
theres no directly read-until-newline-or-space function in the standard libraries of c#, but there is something close:

StreamReader reader=new StreamReader("somefile.txt"); //initializes the reader
string line;//line to be parsed
while((line=reader.ReadLine())!=null)//put current line into line string; make sure it is not EOF.
{
//some line parsing.. split lines into sections,ect.ect....
}




EDIT: google "string tokenizer c#" to parse the strings. Or do it yourself. Its not that hard.

Share this post


Link to post
Share on other sites
Quote:
Original post by supercoder74
EDIT: google "string tokenizer c#" to parse the strings. Or do it yourself. Its not that hard.

Or just use string.split()

Share this post


Link to post
Share on other sites
Quote:
Original post by _DarkWIng_
Quote:
Original post by supercoder74
EDIT: google "string tokenizer c#" to parse the strings. Or do it yourself. Its not that hard.

Or just use string.split()


Here's an example of using that function.

As for the first post, here's another idea:

// Create a new stream reader from the text file
StreamReader strRead = new StreamReader("Test.txt");

// Read in the entire file at once
string strFile = strRead.ReadToEnd();

// Set the parsing characters
char [] splitter = { ' ', '\n', '\t', '\r' };

// Split the string into your tokens
string[] arInfo = strFile.Split(splitter);

// Now loop though and do whatever you want with them
for (int x = 0; x < arInfo.Length; x++)
{
// arInfo [x] contains each token
Console.WriteLine(arInfo [x]);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!