IOStream运算符<<从C ++到C#的转换 [英] IOStream operator<< Conversion from C++ to C#

查看:43
本文介绍了IOStream运算符<<从C ++到C#的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些旧的C ++代码转换为C#,并且难以理解以下代码的作用以及如何将其转换为C#.

I am trying to convert some old C++ code to C# and having difficulty in understanding what the following code does and how can it be converted into C#.

ifstream fin;
fin.open(file, ios::nocreate);

if (!fin)
{
    m_iErrorNumber = 1567;
    num = 0.0;
}
else
{
    for (int x = 0; x < count; x++)
    {
        fin >> num;  //  <==== THIS LINE IS PROBLEM!!
    }
};

fin.close();

推荐答案

这可能与C ++代码的语义略有不同,但是应该相对相似:

This has probably slightly different semantics from the C++ code, but should be relatively similar:

IEnumerable<string> ReadWhiteSpaceSeparated(string filename)
{
    using(var lines = File.ReadLines(filename))
    {
        return lines.SelectMany(line => line.Split(new []{' ','\t', '\r', '\n'}, StringSplitOptions.RemoveEmptyEntries));
    }
}

IEnumerable<string> ReadDoubles(string filename)
{
     return ReadWhiteSpaceSeparated(filename)
         .Select(s => double.Parse(s, CultureInfo.InvariantCulture));
}

然后您可以使用 ReadDoubles(filename)从文件中读取 count 个double.Take(count)

Then you can read count doubles from a file with ReadDoubles(filename).Take(count)

这篇关于IOStream运算符&lt;&lt;从C ++到C#的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆