   [.net] Reading INTs from a text file?
  I've been looking through the .NET documentation trying to figure out how to do the equivalent of this in C##include <fstream>ifstream fin("text.txt");int x;fin >> x;I read about FileStream, and StreamReader. But StreamReader seems to only read characters and arrays of characters. Do I have to convert what I've read into an int, or is there a function I can use to automaticly read it into an integer?   
  The stream readers are also capable of reading in an array of bytes, which could then be converted to a integer. That seems kind of hacky though. I would expect .net to have a more elegant solution.../me searches MSDNAh here it is, BinaryRead and BinaryWriter are Stream classes that have members like ReadInt16 and ReadInt32 on the reader, and WriteInt16 and WriteIn32 on the writer.The BinaryReader and BinaryWriter should do what you seek.   ;  A BinaryReader is probably more suited to binary files.  You're reading in a text file.Look at using System.Convert.ToInt32(), and other functions in System.Convert, to convert from a string that you read in (which contains the number) to an int datatype.See here, here and here.   ;  Thanks, the only problem with those functions is that they terminate on '\r' or '\n'. Do you know if there's a way to terminate on whitespace like the C++ ifstream >> operator?The solution I came up with is:StreamReader File = new StreamReader("text.txt");string Temp = File.ReadToEnd();Temp.Replace(' ', '\r');StringReader Reader = new StringReader(Temp);//Then for the conversion to intint x = Convert.ToInt32(Reader.ReadLine());But it seems really ugly, I expected there to be an easy way to do this with .NET, since it's so easy in C++ using <fstream>   ;  oops double post.Quote:Original post by bgreenA BinaryReader is probably more suited to binary files.  You're reading in a text file.Look at using System.Convert.ToInt32(), and other functions in System.Convert, to convert from a string that you read in (which contains the number) to an int datatype.See here, here and here.I don't think that works. The BinaryReader is for use with binary files, it doesn't do text to integer conversions.   ;  When gettingnumber data from a text file this always works, assuming each number is on it's own line.  I'm sure it could be adapted for a file that's nt that way, though.StreamReader sr = new StreamReader("blah.txt");int x;x = Int32.Parse(sr.ReadLine());Hope that helps.-AJ;  Wait, Why not use the binary reader?The code in the origional post sure looks like its reading an int from a data file. Should I have assumed that the contents of the file are text because of the name "test.txt"?If it is text and the integer is stored in the file as the string "42" then, you are going to have to read in a string, and convert it. But if the file contains the bytes 0,0,0,42 and you want to read a 32 bit integer, then binary reader is the obvious way to go. Here is another question. Is the file you are trying to read something you have control over the format, or are you trying to read in someone else's data? If its your own data, then adjusting the format, could result in a more elegant solution then reading in loads of strings, and then munging and converting left and right.;Quote:Original post by Procyon LotorThanks, the only problem with those functions is that they terminate on '\r' or '\n'. Do you know if there's a way to terminate on whitespace like the C++ ifstream >> operator?The solution I came up with is:*** Source Snippet Removed ***But it seems really ugly, I expected there to be an easy way to do this with .NET, since it's so easy in C++ using <fstream>What's the format of the file you're reading? If we know that we can suggest a better solution.   ;  Yes, it's in plain text and is going to be a mix of strings and integers. I don't really have much choice as to the format of the file. It's going to look like this."From 5 6 To 8 5From 7 1 To 71 47" ...etcThe file will be pairs of points defining a line. Each pair is preceeded by the word "From" and is devided by the word "To", there will be one or more spaces seperating the elements of each point, also there optionally can be a comma seperating them like "From 6, 3 To 11, 53"   ;  In that case, why not use the regular expression library (edit: if you happen to know regular expressions of course...; otherwise, here's what they can do. They are neat.)? A quickie job, you'll likely want to properly functionize things:class Program {   static void Main(string[] args) {  System.Text.RegularExpressions.Regex Rx = new System.Text.RegularExpressions.Regex(@"From\s+(\d+)(?:(?:,?\s*)|(?:\s+))(\d+)\s+To\s+(\d+)(?:(?:,\s*)|(?:\s+))(\d+)");  string test = "From 1 2 To 3 4";  string test2 = "From 5,6 To 7,  8";  System.Drawing.Point p1, p2, p3, p4;  System.Text.RegularExpressions.Match Mch;  Mch=Rx.Match(test);  if (Mch.Success) { p1 = new System.Drawing.Point(int.Parse(Mch.Groups[1].Value), int.Parse(Mch.Groups[2].Value)); p2 = new System.Drawing.Point(int.Parse(Mch.Groups[3].Value), int.Parse(Mch.Groups[4].Value));  }  Mch = Rx.Match(test2);  if (Mch.Success) { p3 = new System.Drawing.Point(int.Parse(Mch.Groups[1].Value), int.Parse(Mch.Groups[2].Value)); p4 = new System.Drawing.Point(int.Parse(Mch.Groups[3].Value), int.Parse(Mch.Groups[4].Value));  }    }    }Maybe not so much better than manually extracting parts with such simple syntax, but generally more robust.   