我有一個Fortran程序,從這樣的文件中讀取數據的最佳方式:什麼是C#實現這個Fortran語言片斷
10 READ(X,*,ERR=8000,END=9000) ... Read header line of data sequence
C Some processing of header line data...
READ(X,*,ERR=8000) ... Read secondary line of data sequence
C Some processing of secondary line data...
20 READ(X,*,ERR=8000) ... Read data line
IF data contains 'X' GOTO 10
C Do some calculations with the data ...
IF (X.LT.Y)
GOTO 10
C Do some more calculations ...
100 CONTINUE
8000 'Output some error to the log'
9000 'Some final stuff'
RETURN
原代碼是很多比這更長的時間,但是這是要旨。 我在想像下面的C#代碼應該做同樣的事情(從內存編碼,所以可能會有一些錯誤...),但出於某種原因,這似乎極其複雜,以達到相同的結果。是否有複製Fortran例程流的簡單方法?只是使用gotos提供比使用代碼塊更短的代碼?
private void MyFunction(Stream MyData)
{
string s = string.Empty;
bool flag;
StreamReader sr = new StreamReader(MyData);
try
{
while (!sr.EndOFStream)
{
s = sr.ReadLine(); ... Read header line of data sequence
//Some processing of header line data ...
s = sr.Readline(); ... Read secondary line of data sequence
//Some processing of secondary line data ...
flag = false;
while (!(s = sr.ReadLine()).Contains("X"))
{
//Do some calculations with the data ...
if (X < Y)
{
flag = true;
break;
}
//Do some more calculations ...
}
if (flag) continue;
}
//Some final stuff ...
return;
}
catch
{
//Output error to log...
}
}
謝謝,這是非常有幫助的。 –