简体   繁体   English

将iostream输入代码从C ++移植到C#

[英]Porting iostream input code from C++ to C#

This is C++ code for reading traces of address of main memory for cache memory simulation: 这是用于读取主存储器地址跟踪的C ++代码,用于高速缓存存储器仿真:

 char hex[20];
 ifstream infile;
 infile.open(filename,ios::in);
 if(!infile) {
    cout<<"Error! File not found...";
    exit(0);
 }
 int set, tag, found;
 while(!infile.eof()) { //Reading each address from trace file
      if(base!=10) {
           infile>>hex;
           address = changebase(hex, base);
      } else {
           infile>>address;
      }
      set = (address / block_size) % no_set;
      tag  = address / (block_size * no_set);
 }

I have converted this to C# code: 我已将其转换为C#代码:

 char[] hex = new char[20];
 FileStream infile=new FileStream(filename, FileMode.Open);

 if (infile == null) {
     Console.Write("Error! File not found...");
     Environment.Exit(0);
 }
 int set;
 int tag;
 int found;
 while (!infile.CanRead) { //Reading each address from trace file
     if (@base != 10) {
         infile >> hex;
         address = changebase(hex, @base);
     } else {
         infile >> address;
     }
     set = (address / block_size) % no_set;
     tag = address / (block_size * no_set);
 }

The problem is on line infile >> hex; 问题是在线infile >> hex; C# is giving syntax errors, as shift right operator cannot be applied to string operators. C#给出语法错误,因为右移运算符不能应用于字符串运算符。

Why this is not working? 为什么这不起作用? I'm making a small cache hit and miss calculation project. 我正在制作一个小型缓存命中计算项目。

To quantify what Eric means: 量化Eric意味着什么:

C++ is quite flexible in the operators that can be overloaded. C ++在可以重载的运算符中非常灵活。 It has become an "idiom" that the bitshift operators << and >> also be used for input and output. 比特变换操作符<<>>也用于输入和输出已成为“习语”。 This actually makes kind of sense as it is a logical construct and the eye registers some kind of "flow" between objects. 这实际上是有道理的,因为它是一个逻辑构造,眼睛在对象之间注册某种“流”。

In C#, you don't overload those operators. 在C#中,您不会重载这些运算符。 What Eric means is, you need to say explicitly, on a stream object, to write (or indeed, read) something. Eric意味着,你需要在流对象上明确地说,写(或实际上,读)某些东西。 This means calling the methods directly. 这意味着直接调用方法。

In essence you're doing the same thing - the operator overloading is just a nice shortcut, but at the end of the day some method is going to be called - be it a nice decorative "operator overload" or a plain old function call with a name. 从本质上讲,你正在做同样的事情 - 运算符重载只是一个很好的捷径,但在一天结束时会调用一些方法 - 它是一个很好的装饰“运算符重载”或一个普通的旧函数调用一个名字。

So, in C++ we might write: 所以,在C ++中我们可能会写:

std::cout << "Hello" << std::endl;

Whereas in C# we'd write: 而在C#中,我们写道:

Console.WriteLine("Hello");

If we ignore the fact that std::cout could potentially be different from the console window (this is illustrative), the concept is exactly the same. 如果我们忽略了std::cout可能与控制台窗口不同的事实(这是说明性的),那么概念就完全相同了。

To expand on the idea of the operators, you'll also have probably come across things such as stringstream .. a class that acts like a stream for strings. 为了扩展运算符的概念,你可能也会遇到诸如stringstream类的东西......一个类似于字符串流的类。 It's really quite useful: 这非常有用:

std::stringstream ss;
int age = 25;
ss << "So you must be " << age << " years old.";

In C#, we achieve this with the StringBuilder class: 在C#中,我们使用StringBuilder类实现了这一点:

StringBuilder sb = new StringBuilder();
int age = 25;
sb.Append("So you must be ").Append(age).Append(" years old");

They both do exactly the same thing. 他们都做了完全相同的事情。 We could also do: 我们也可以这样做:

sb.AppendFormat("So you must be {0} years old", age);

This is more akin (in my opinion) to the more C-like sprintf methods, or more recently, boost's format library . 对于更像C的sprintf方法,或者最近的boost的格式库 ,这更像是(在我看来)。

C# does not use the bizarre C++ convention that bitshifting also means stream manipulation. C#没有使用奇怪的C ++约定,即bithifting也意味着流操作。 You'll have to actually call methods for I/O. 您必须实际调用I / O方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM