0

Possible Duplicate:
Looking for C# equivalent of scanf

I am trying to convert a line of code from c to c#

fscanf(fp, "%d %d\n", &x,&y);

is there an equivalent function for fscanf() in c#? how do i convert this line of code?

Update: fp is a file pointer to a text file which contains an input like this

0 0

I need to save the integer inputs to x and y, respectively.

Community
  • 1
  • 1

3 Answers3

0
var parts = streamReader.ReadLine().Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
int x = Int32.Parse(parts[0]);
int y = Int32.Parse(parts[1]);
L.B
  • 114,136
  • 19
  • 178
  • 224
  • 2
    Rather than only post a block of code, please *explain* why this code solves the problem posed. Without an explanation, this is not an answer. – Martijn Pieters Oct 12 '12 at 11:40
  • @MartijnPieters This is my style. If you don't like the answer, simply don't upvote(as you do now). *A block of code?* 3 lines? – L.B Oct 12 '12 at 11:45
  • 1
    Your post ended up in the 'low quality posts' queue, and I elected to give feedback. Make whatever you want of that. :-) – Martijn Pieters Oct 12 '12 at 11:49
  • @MartijnPieters It seems OP doesn't think the same :) – L.B Oct 12 '12 at 11:49
-2
String.Format("someFormatString", MyFileStreamReader.ReadLine());
Rob Hardy
  • 1,821
  • 15
  • 15
-2

Check out the BinaryReader class. In C# you would use a Stream object in place of the C FILE pointer. Something like this should work

System.IO.Stream fp = System.IO.File.OpenRead(@"C:\Path\To\My\File.txt");
double x, y;
using (var reader = new System.IO.BinaryReader(fp))
{
    x = reader.ReadDouble();
    y = reader.ReadDouble();
}
dbattaglia
  • 678
  • 4
  • 7
  • No, something like that will not work. It's for reading floats stored in 4 bytes, in binary, not in a human readable string. – Ray Feb 08 '15 at 21:39