从文本文件读取数据,按每一行计算所需参数,并将结果存储在另一个文本文件中 [英] read data from textfile ,calculate required parameter by each row and store result in another textfile

查看:88
本文介绍了从文本文件读取数据,按每一行计算所需参数,并将结果存储在另一个文本文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友
我是C#编程的新手.
我是测量工程师,我想写一个程序来计算一些必需的参数.
我的程序必须读取一个文本文件,该文本文件中的每一行都包含用于测量点坐标的x,y,z,并且程序必须为每个点计算一些参数(通过使用存储在每个点中的点坐标的x,y,z我的问题是如何读取文本文件中的x,y,z,并在计算完参数后将计算出的参数存储在其他文本文件的行中.
例如:
我有一个与以下相同的文本文件:

Hi friends
I’m a newbie in C# programming.
I’m a surveyor engineer and I want write a program that calculates some required parameters.
My program must read a text file , in that text file each row contains x,y,z for coordinates of a survey point and program must calculate some parameters for each points (by using x,y,z of point coordinate that stored in each rows of text file).my question is how can I read x,y,z in text file and after calculating parameter , store calculated parameters in rows of other text file .
For example:
I have a text file same as below:

5,8,6
2,7,9
7,6,3
6,9,4


我想编写一个程序来加载此文本文件,并读取每一行中的每个数字,例如将这3个数字相加,最后将结果存储在另一个文本文件中:


I want to write a program that load this text file and read each numbers of in each line and for example summing this 3 number and finally store result in another text file :

19
18
16
19


谁能帮助我该怎么做?有什么程序可以向我显示如何执行此操作吗?

------------------------------------------
我遇到了另一个问题,如何从文本文件创建多维数组?文本文件具有m行和n列,行中的数字由逗号分隔.实际上,我想从文本文件创建m * n矩阵.
例如,我们有这样的文本文件:
2,3,5,7
4,5,2,1
2,5,8,3
3,4,9,6
1,7,8,9
看起来我必须使用嵌套循环,但是我不知道该怎么办?


Can anyone help me how can I do that ? is there any program that show to me how can I do this ?

------------------------------------------
i have faced another problem , how can i create multidimensional array from text file? text file has m rows and n columns and numbers in rows delimited by comma.in fact i want to create a m*n matrix from text file .
for example we have text file like this :
2,3,5,7
4,5,2,1
2,5,8,3
3,4,9,6
1,7,8,9
looks like i have to use nested loop but i have no idea how can i do this ??

推荐答案

我在某种程度上同意SAKryukov的观点.但是,我发现您拥有完全不同的专业,并且不想学习编码而是学习解决特定任务.所以,让我给您一些提示.

File.ReadAllLines将以字符串数组的形式为您提供文件,每行一个.您可以使用foreach遍历该集合.

string.Split可用于分割您的字符串,例如逗号上的5,8,6,从而形成三个字符串的数组,其中一个包含5个字符串,一个包含8个字符串,一个包含6个字符串.这些仍然是字符串tho.

int.TryParse会将每个字符串转换为数字.

StringBuilder可用于构建包含所需新格式数据的新字符串.

File.WriteAllText会将您的数据写出到文件系统中.
I agree with SAKryukov, in part. However, I see that you have a completely different speciality and don''t want to learn coding so much as learn to resolve a specific task. So, let me give you some pointers.

File.ReadAllLines will give you your file as an array of strings, one per line. You can use foreach to iterate over that collection.

string.Split can be used to split your string, such as 5,8,6 on the comma, resulting in an array of three strings, one with 5 in it, one with 8 in it and one with 6. These are still strings tho.

int.TryParse will convert each string in to a digit.

a StringBuilder can be used to build a new string that contains the data in the new format you want.

File.WriteAllText will write your data out to the file system.


这篇关于从文本文件读取数据,按每一行计算所需参数,并将结果存储在另一个文本文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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