这是用于读取高速缓存内存模拟的主内存地址跟踪的 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);
}
我已将其转换为 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);
}
问题在于infile >> hex;
C# 行给出了语法错误,因为右移运算符不能应用于字符串运算符。
为什么这不起作用?我正在做一个小的缓存命中和未命中计算项目。