保存来自文本文件的数据 [英] To save data which comes from a text file

查看:55
本文介绍了保存来自文本文件的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我的代码:



 StreamReader reader_forces =  new  StreamReader (  Suj01_PC_DP_M86_forces.txt); 

使用(reader_forces){

string line_f = ;

for int m = 0 ; m < 8 ; m ++){
line_f = reader_forces .ReadLine();
}

data_F = new string [ 80 ] [];

float Somme_x = 0 ;
float Somme_y = 0 ;
float Somme_z = 0 ;

int counter_F2 = 0 ;
while (line_f!= null ){
counter_F2 ++;
line_f = reader_forces.ReadLine();

if (line_f!= null && line_f.Length < span class =code-keyword><
3
break ;

if ((counter_F2% 10 )!= 0 ){
Debug.Log(cpt_F); // 我有:0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 ... 28 28 28 28 28 28 28 28 28 28因此cpt_f的每个值重复10次
Debug.Log(line_f); // 我有所有显示的行,每次

< span class =code-keyword> string [] split_data_F;
split_data_F = line_f.Split( new string [] { \t},StringSplitOptions.RemoveEmptyEntries); // 这里我有以下错误消息:NullReferenceException:对象引用未设置为对象的实例

if (split_data_F [ 3 ]。等于( NaN))
continue ;

Somme_x + = float .Parse(line_f.Split( new string [] { \t} ,StringSplitOptions.RemoveEmptyEntries)[ 1 ]);
Somme_y + = float .Parse(line_f.Split( new string [] { \t},StringSplitOptions.RemoveEmptyEntries )[ 2 ]);
Somme_z + = float .Parse(line_f.Split( new string [] { \t},StringSplitOptions.RemoveEmptyEntries )[ 3 ]);


} else {

data_F [cpt_F] = line_f.Split( new string [] { \t},StringSplitOptions.RemoveEmptyEntries);

Somme_x / = 9 ;
Somme_y / = 9 ;
Somme_z / = 9 ;

data_F [cpt_F] [ 1 ] = Somme_x.ToString();
data_F [cpt_F] [ 2 ] = Somme_y.ToString();
data_F [cpt_F] [ 3 ] = Somme_z.ToString();

Somme_x = 0 ;
Somme_y = 0 ;
Somme_z = 0 ;

cpt_F ++;
}

}







我的问题在于评论。



目的是每组10行,并计算每组的平均值。



提前谢谢!

解决方案

当进行调用的对象为null时,抛出NullReferenceException。



检查调用Split的变量(lines_data,line_f)是否为空。


你没告诉我们在哪里发生错误,但我认为它是在线:

 split_data_F = line_f.Split( new   string  [] {  \t },StringSplitOptions.RemoveEmptyEntries); 

if (split_data_F [ 3 ]。等于( NaN))
继续;



您假设 Split 命令将返回至少4个项目,但如果没有,或者该怎么办,或者不归还什么?您需要检查 split_data_F 是否不是 null ,以及它包含的元素数量。永远不要假设数据有效。


你的问题正如上面的goodpeapul所述(我错过了)。你有两个循环,你从循环中的文件中读取,但无法测试数据结束(从读取返回null)。您需要更改以下代码:

  while (line!=  null ){
counter ++;
line = reader_trajectoires.ReadLine();

// 更改为

while ((line = reader_trajectoires.ReadLine())!= null ){
counter ++;





  //  检查这是否正确 
int m = 0 ; m < 7 ; m ++){
line_f = reader_forces.ReadLine();
}





  while (line_f!=  null ){
counter_F2 ++;
line_f = reader_forces.ReadLine();

// 更改为

while ((line_f = reader_forces.ReadLine())!= null ){
counter_F2 ++;


Here my code :

		StreamReader reader_forces = new StreamReader ("Suj01_PC_DP_M86_forces.txt"); 
		
		using (reader_forces) {

			string line_f = " ";

						for (int m = 0; m < 8; m++) { 
								line_f = reader_forces.ReadLine ();
						}
			
						data_F = new string[80][]; 
			
						float Somme_x = 0;
						float Somme_y = 0;
						float Somme_z = 0;
			
						int counter_F2 = 0;
						while (line_f != null) { 
								counter_F2++;
								line_f = reader_forces.ReadLine ();  

								if (line_f != null && line_f.Length < 3)
									break;
								
								if ((counter_F2 % 10) != 0) { 
Debug.Log (cpt_F); // I have : 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 ... 28 28 28 28 28 28 28 28 28 28 so each value of cpt_f is repeated 10 times
Debug.Log (line_f); // I have the all lines which are displayed, once each

								string [] split_data_F;
								split_data_F = line_f.Split (new string[] {"\t"}, StringSplitOptions.RemoveEmptyEntries); // Here I have this error message : NullReferenceException: Object reference not set to an instance of an object

					if (split_data_F [3].Equals ("NaN"))
						continue;
					
								Somme_x += float.Parse (line_f.Split (new string[] {"\t"}, StringSplitOptions.RemoveEmptyEntries) [1]); 
								Somme_y += float.Parse (line_f.Split (new string[] {"\t"}, StringSplitOptions.RemoveEmptyEntries) [2]);
								Somme_z += float.Parse (line_f.Split (new string[] {"\t"}, StringSplitOptions.RemoveEmptyEntries) [3]);


								} else {
					
										data_F [cpt_F] = line_f.Split (new string[] {"\t"}, StringSplitOptions.RemoveEmptyEntries); 

										Somme_x /= 9;
										Somme_y /= 9;
										Somme_z /= 9;
					
										data_F [cpt_F] [1] = Somme_x.ToString ();
										data_F [cpt_F] [2] = Somme_y.ToString ();
										data_F [cpt_F] [3] = Somme_z.ToString ();
					
										Somme_x = 0;
										Somme_y = 0;
										Somme_z = 0;
					
										cpt_F ++;
								}

						}




My problem is indicated in comments.

Mu purpose is to take a group of 10 lines at each time, and to calculate the average of each group.

Thank you in advance !

解决方案

NullReferenceException is thrown when the object that is making the call is null.

Check if the variables (lines_data, line_f) that are calling "Split" are not null.


You did not tell us where the error occurs, but I am assuming it is at the lines:

split_data_F = line_f.Split (new string[] {"\t"}, StringSplitOptions.RemoveEmptyEntries);
 
if (split_data_F [3].Equals ("NaN"))
    continue;


You are making the assumption that the Split command will return at least 4 items, but what if it does not, or does not return anything? You need to check that split_data_F is not null, and also how many elements it contains. Never assume that the data will be valid.


Your problem is as described by goodpeapul above (which I missed). You have two loops where you read from your file inside the loop, but fail to test for end of data (null returned from read). You need to change the following pieces of code:

while (line != null) { 
    counter++;
    line = reader_trajectoires.ReadLine ();

// change to 

while ((line = reader_trajectoires.ReadLine ()) != null) { 
    counter++;



// check that this is correct
for (int m = 0; m < 7; m++) { 
    line_f = reader_forces.ReadLine (); 
}



while (line_f != null) { 
    counter_F2++;
    line_f = reader_forces.ReadLine (); 

// change to

while ((line_f = reader_forces.ReadLine ()) != null) { 
    counter_F2++;


这篇关于保存来自文本文件的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆