while( fin >> currentWord )
{
process word....
}
c# text file loading
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
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.
theres no directly read-until-newline-or-space function in the standard libraries of c#, but there is something close:
EDIT: google "string tokenizer c#" to parse the strings. Or do it yourself. Its not that hard.
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.
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()
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
Popular Topics
Advertisement