小编Dea*_*die的帖子

负平方根

你如何在C++中取负数的平方根?
我知道它应该返回一个真实而复杂的部分,我得到一个NaN?
我该如何看待真实的部分?

c++ math sqrt

4
推荐指数
2
解决办法
9988
查看次数

读取多个文件

我想交替读取多个文件。下面是我的代码的简化版本。

ifstream* In_file1 = new ifstream("a.dat",  ios::binary);
ifstream* In_file2 = new ifstream("b..dat",  ios::binary);
ifstream* In_file;
int ID;

In_file = In_file1; 
int ID = 0;

//LOOPING PORTION
if (In_file -> eof())
{
  In_file -> seekg(0, ios_base::beg);
  In_file->close();

  switch (ID)
   {
     case 0:
           In_file = In_file2;  ID = 1; break;
     case 1:
          In_file = In_file1; ID = 0;  break;
   }
}
//some codes
:
:
In_file->read (data, sizeof(double));

//LOOPING PORTION
Run Code Online (Sandbox Code Playgroud)

如果我一次阅读这些文件并且我认为一切都很酷,那么该代码运行良好。但是,如果称为“循环部分”的部分位于循环内,则行为会变得很奇怪,并且我开始有一个重复输出。请问,有人可以告诉我出了什么问题以及如何解决它吗?如果您有更好的解决问题的方法,请提出。我很感激。

//解决了

谢谢大家的评论,我很感激。这是我简单做的:

而不是原来的

switch (ID)
{
  case 0:
   In_file = In_file2; …
Run Code Online (Sandbox Code Playgroud)

c++ ifstream

1
推荐指数
1
解决办法
4228
查看次数

标签 统计

c++ ×2

ifstream ×1

math ×1

sqrt ×1