且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从C#中的文件中读取大整数整数?

更新时间:2023-11-10 13:58:28

Quote:

未处理的异常:System.OverflowException:对于Int32,值太大或太多

Unhandled Exception: System.OverflowException: Value was either too large or too
small for an Int32.

你可能有一个非常大的整数适合32位整数。

您需要切换到无符号整数 long 数据类型。



要知道什么是给您带来问题的价值,请在for循环中添加一行

You probably have a very large integer that don't fit in a 32 bits integer.
You need to switch to unsigned integer or long data type.

To know what is the value that give you a problem, add a line in the for loop

string LastInteger= integerStrings[n];



,调试器会告诉你什么是价值。



调试器允许你逐行跟踪执行,检查变量,你会发现有一点它会停止你所期望的。

Debugger - ***,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么以及您的代码任务是与它应该做的事情进行比较。

如果代码没有达到预期的效果,你就接近一个bug。


and the debugger will show you what is the value.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.


我使用c ++填充此文件时没有任何间距,因此它将其识别为一个int

并且有你的问题。

如果你没有空格(或逗号)分开数据,没有人能分辨12345678901234567890是12,345,6 ,7,89,...或者1,2,3,4567890123456,...

因此系统会尝试将其读作一个巨大的整数而失败。



你需要回到C ++代码并生成更多明智的数据:要么是单独一行上的每个值,要么是逗号分隔(或者如果有一些组织则***混合两者)在数据中。
"I fill this file using c++ without any spacing so it recognizes it as one int"
And there is your problem.
If you don't space (or comma) separate the data, nobody can tell if 12345678901234567890 is 12, 345, 6, 7, 89, ... or 1, 2, 3, 4567890123456, ...
So the system tries to read it as one huge integer and fails.

You need to go back to the C++ code and generate more "sensible" data: either each value on a separate line, or comma separated (or best a mix of both if there is some "organisation" in the data).


我实际上找到了解决方案:)



I actually found the solution :)

for (int i = 0; i < maxgeneration; i++)
              for (int j = 0; j < popsize; j++)
                  for (int k = 0; k < genesize; k++)
                  {
                      bact_array[i,j,k] = (int)char.GetNumericValue(text_b[b]);
                      b++;
                  }







我用这个方法,我读字符所以我需要转换它们to the int



int i =(int)char.GetNumericValue(c);




I used this method, I read chars so I need to convert them to ints

int i = (int)char.GetNumericValue(c);