Click here to Skip to main content
15,913,836 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
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

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

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 ??
Posted
Updated 3-Jul-11 18:48pm
v3
Comments
Sergey Alexandrovich Kryukov 30-Jun-11 19:25pm    
If you're are a newbie, start with your own implementation, even if you're not sure you can do it. It will later help you to really understand answers when you face with problems. Then report those problems; we would gladly help.
--SA
Wonde Tadesse 30-Jun-11 19:46pm    
Agree. This is how you learn things.
ghahremanimehdi 30-Jun-11 20:28pm    
at least tell me how can i use arrays to handle this problem ?
Christian Graus 30-Jun-11 20:38pm    
See my answer below.

1 solution

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.
 
Share this answer
 
Comments
ghahremanimehdi 30-Jun-11 21:28pm    
Thanks for your useful response.is there any example to give me an idea ?I'm in a big hurry!i have to implement as soon as possible.
Christian Graus 30-Jun-11 21:42pm    
I suggest you work through my suggestions, post your code and ask specific questions for help. I don't really have time to write this for you, but if you post some code b/c you're stuck, I will look at it, as will others here.,
Sachin__Sharma 4-Jul-11 0:34am    
If you better know about the problem then why do you not help him clearly put your answer with a small example.
Sergey Alexandrovich Kryukov 3-Jul-11 0:32am    
Agree, my 5. These directions suggest a good way to get started.
--SA
ghahremanimehdi 4-Jul-11 0:51am    
OK . my new question merged with the first question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900