c# text file loading

Started by
2 comments, last by Drew_Benton 18 years, 10 months ago
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.
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 readerstring line;//line to be parsedwhile((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.
I program in my sleep,but when I sleep I use the partition in my head that doesnt have g++ or the .net library, so im kinda screwed.
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()
You should never let your fears become the boundaries of your dreams.
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 fileStreamReader strRead = new StreamReader("Test.txt");// Read in the entire file at oncestring strFile = strRead.ReadToEnd();// Set the parsing characterschar [] splitter = { ' ', '\n', '\t', '\r' };// Split the string into your tokensstring[] arInfo = strFile.Split(splitter);// Now loop though and do whatever you want with themfor (int x = 0; x < arInfo.Length; x++){   // arInfo [x] contains each token   Console.WriteLine(arInfo [x]);}

This topic is closed to new replies.

Advertisement