无法看到streamreader的问题 [英] Can't see the problem with streamreader
问题描述
我有一个流阅读器。它从包含两列的文件一次读取一行。第一个是整数值,第二个是double。我需要将它们分成二维阵列的单独侧面。 [int] [double]但是为了让事情变得更容易,2D数组保持为双倍 - 我只需要将数字分开以供日后使用。这就是我所拥有的:
I have a stream reader. It reads a line at a time from a file that contains two columns. The first is an integer value and the second is double. I need to seperate these into seperate sides of a 2 dimensional array. [ int ] [ double ] but to make things easier the 2D array is kept as double - I just need the numbers seperate for later on. This is what I have:
String^ File = "C:\\Users\\**** :) secret ****\\Documents\\Error.txt";
StreamReader^ sr = File::OpenText(File);
String^ str;
bool Flip = true;
int index = 0;
while ((str = sr->ReadLine()) != nullptr)
{
// initiates a stringbuilder of 50 characters
StringBuilder^ sb = gcnew StringBuilder("", 50);
for(int i=0; i < str->Length; i++)
{// for each char in line of string str if it is a digit or decimal place then...
if(Char::IsDigit( str[i]) || str[i] == '.')
sb->Append( str[i] ); // append to stringbuilder
else {
// otherwise convert the stringbuilder to a double number
double Num = Convert::ToDouble( sb );
Flip = !Flip; // decides which 'side' of 2d array to place in
if(!Flip) 2dArray[index, 0] = Num; // column 1
else 2dArray[index, 1] = Num; // column 2
// remove all elements of string clear for rest of line read.
sb->Remove(0, sb->Length-1);
}
}
index++; // increments for array index
}
为了测试这个,我把数字放在for循环中以找到最大值和最小值。然后将它们发送到文本框以供显示。数据完全不变......表示此代码存在问题。这封装在try {} catch {}中。省略这个告诉我
Num =转换:: ToDouble(sb);
isn'不允许。那么我还有什么选择呢?编译器没问题:(
非常感谢
To test this I''ve put the numbers through a for loop to find the max and minimum. These are then sent to textboxes for display. The data is completely unchanged...indicating there is a problem with this code. This was encapsulated inside a try {} catch{}. Omitting this showed me that the
Num = Convert::ToDouble( sb );
isn''t allowed. So what alternative do I have? No problem came up with the compiler :(
Many thanks
推荐答案
它不是很很好的代码,正如谢尔盖(SA)所说,但它是可以解决的。虽然SA是正确的,它是一个解决问题的坏方法,并且使用string.Split将是一个更整洁,更易读的解决方案,你遇到的问题是没有Convert.ToDouble重载,它接受一个StringBuilder,因此它作为一个Object传递,这可能会导致问题。使用ToString转换StringBuilder将解决这个问题。
但你应该使用Double.Parse,因为它只需要一个字符串,问题从一开始就更加明显!
还有另外一个问题但是你的方法是:除非你的行以非数字值结尾,否则你每次只能提取一个数字......:笑:
It''s not very nice code, as Sergey (SA) says, but it is fixable. While SA is right that it is a bad solution to the problem, and using string.Split would be a neater and more readable solution, the problem you have met is that there is no Convert.ToDouble overload that takes a StringBuilder so it is passed as a Object and that probably causes the problem. Using ToString to convert the StringBuilder would solve that problem.
But you should use Double.Parse instead, as it only takes a string and the problem would have been more obvious right from the start!
There is another problem with your approach though: unless your line ends with a non digit value, you will only extract one number each time...:laugh:
这篇关于无法看到streamreader的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!