从C#的文件读取大整数整数
问题说明
你好,我想读取文件并将其放入1D int数组中我该怎么办?
我试过以下但是它给了我一个整数溢出。我想读的只是整数而不是字符。
我得到的错误是:
未处理的异常:System.OverflowException:Value是要么对于Int32来说太大或太多
很小。
在System.Number.ParseInt32(字符串s,NumberStyles样式,NumberFormatInfo在
fo中)
at System.Int32.Parse(String s)
at tofile_2.Program.Main(String [] args)in c:\users\user\documents \ visual stu
dio 2015 \Projects\tofile_2 \tofile_2 \Program.cs:80行
谢谢!
我的尝试:
hello, I want to read file and put it into 1D int array how can I do it?
I have tried the following but it gives me an integer overflow. I want to read just integers not chars.
the error what I get is:
Unhandled Exception: System.OverflowException: Value was either too large or too
small for an Int32.
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo in
fo)
at System.Int32.Parse(String s)
at tofile_2.Program.Main(String[] args) in c:\users\user\documents\visual stu
dio 2015\Projects\tofile_2\tofile_2\Program.cs:line 80
Thanks!
What I have tried:
string fileContent = File.ReadAllText(@"C:/Users/user/Desktop/deskt/bacteria.txt");
string[] integerStrings = fileContent.Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
int[] integers = new int[integerStrings.Length];
for (int n = 0; n < integerStrings.Length; n )
integers[n] = int.Parse(integerStrings[n])
正确答案
未处理的异常: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);
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcajekk
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20