This is something that should be very simple. I just want to read numbers and words from a text file that consists of tokens separated by white space. How do you do this in C#? For example, in C++, the following code would work to read an integer, float, and word. I don't want to have to use a regex or write any special parsing code.
ifstream in("file.txt");
int int_val;
float float_val;
string string_val;
in >> int_val >> float_val >> string_val;
in.close();
Also, whenever a token is read, no more than one character beyond the token should be read in. This allows further file reading to depend on the value of the token that was read. As a concrete example, consider
string decider;
int size;
string name;
in >> decider;
if (decider == "name")
in >> name;
else if (decider == "size")
in >> size;
else if (!decider.empty() && decider[0] == '#')
read_remainder_of_line(in);
Parsing a binary PNM file is also a good example of why you would like to stop reading a file as soon as a full token is read in.
Try someting like this:
http://stevedonovan.blogspot.com/2005/04/reading-numbers-from-file-in-c.html
IMHO Maybe to read a c# tutorial it will be really useful to have the whole picture in mind before asking
I like using the StreamReader for quick and easy file access. Something like....
Not exactly the answer to your question, but just an idea to consider if you are new to C#: If you are using a custom text file to read some configuration parameters, you might want to check XML serialization topics in .NET.
XML serialization provides a simple way to write and read XML formatted files. For example, if you have a configuration class like this:
you can simply save it and load it using the
XmlSerializer
class:Save
method as defined above will create a file with the following contents:Good thing about this approach is that you don't need to change the
Save
andLoad
methods if yourConfiguration
class changes.C# doesn't seem to have formatted stream readers like C++ (I would be happy to be corrected). So Jon Skeet approach of reading the contents as string and parsing them to the desired type would be the best.
Brannon's answer explains how to read binary data. If you want to read text data, you should be reading strings and then parsing them - for which there are built-in methods, of course.
For example, to read a file with data:
You might use:
Note that this has no error handling. In particular, it will throw an exception if the file doesn't exist, the first two lines have inappropriate data, or there are less than two lines. It will leave a value of
null
inz
if the file only has two lines.For a more robust solution which can fail more gracefully, you would want to check whether
reader.ReadLine()
returnednull
(indicating the end of the file) and useint.TryParse
anddouble.TryParse
instead of theParse
methods.That's assuming there's a line separator between values. If you actually want to read a string like this:
then the code would be very similar:
Again, you'd want to perform appropriate error detection and handling. Note that if the file really just consisted of a single line, you may want to use
File.ReadAllText
instead, to make it slightly simpler. There's alsoFile.ReadAllLines
which reads the whole file into a string array of lines.EDIT: If you need to split by any whitespace, then you'd probably be best off reading the whole file with
File.ReadAllText
and then using a regular expression to split it. At that point I do wonder how you represent a string containing a space.In my experience you generally know more about the format than this - whether there will be a line separator, or multiple values in the same line separated by spaces, etc.
I'd also add that mixed binary/text formats are generally unpleasant to deal with. Simple and efficient text handling tends to read into a buffer, which becomes problematic if there's binary data as well. If you need a text section in a binary file, it's generally best to include a length prefix so that just that piece of data can be decoded.