Dear all
I want to read a file ("test.txt") tha contains the following line and put the numbers in an array
" A 1.3 1.2 2.5 B"
As you can see the spaces between the characters are random
I know that it is extremely is to be done in C with the following way
float A[3];
char x1,x2;
fp1=fopen("test.txt","r");
fscanf(fp1,"%c %f %f %f %c \n",&x1,&A[0],&A[1],&A[2],&x2);
fclose(fp1);
Can someone tell me how can I do it in C#?
Thanks in advance
Chrysostomos
Loading
Jaish MathewsPosted Jul 26, 2010, 3:22 PM
Hi,
Try this
static void Main(string[] args)
{
StreamReader sr = File.OpenText(@"c:\test.txt");
string line = sr.ReadLine();
string[] items = line.Split(' ');
string[] numbers = new string[3];
int index =0;
foreach (string item in items)
{
float result = 0;
bool flag = float.TryParse(item, out result);
if (flag)
{
numbers[index] = item;
index++;
}
}
}