Modify/write txt file

Apr 22 2014 11:47 AM
Hello Everyone!

I'm a bit stuck with something here and I hope you can help me out guys! My problem is that I have to modify only some specific details in a txt file (actually it is a wrl file but it can be considered as a txt file). There is a lot of text there but I just need to modify the point coordinates and leave the rest of the text as it is. Reading the "sample file" at some stage we will see some blocks that look like the below:

Blah blah blah...
 
coord Coordinate {
point [
# bottom
-1.0 -1.0 1.0, #vertex 0
1.0 -1.0 1.0, #vertex 1
1.0 -1.0 -1.0, #vertex 2
-1.0 -1.0 -1.0, #vertex 3
# top
0.0 1.0 0.0 #vertex 4
]
}
 
blah, blah, blah...

where the numbers are (X,Y,Z) coordinates. What I have to do is to change those figures for (X,0,sqrt(X^2+Z^2)) points. Therefore the expected result (test file) will be as follows:

blah, blah, blah...
 coord Coordinate {
point [
# bottom
-1.0 0.0 1.4142, #vertex 0
1.0 0.0 1.4142, #vertex 1
1.0 0.0 1.4142, #vertex 2
-1.0 0.0 1.4142, #vertex 3
# top
0.0 0.0 0.0 #vertex 4
]
}
 
blah, blah, blah...

So in my opinion it can be good to have two files: sample.txt and test.txt where the sample file will never be modified whereas the test.txt will be the same as sample.txt but including the modifications as explained above. The idea can be to read the sample.txt, store the info and then start copying each line in the test file until we get to the points coordinates (which needs to be modified) and continuying with this process until the next set of coordinates.

The problem is that I do not know how to change x,y,z coordinates from the sample.txt to (X,0,sqrt(X^2+Z^2)) and to copy them in the test.txt. Please see some code below.

If you could give me a hand with this I would be extremely greatful!
Many thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//Read a Text File///////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.IO;
 
namespace readwriteapp
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
         
            String line;
 
        try
            {
                //Pass the file path and file name to the StreamReader constructor
                StreamReader sr = new StreamReader("C:\\Sample.txt");
 
                //Read the first line of text
                line = sr.ReadLine();
 
                //Continue to read until you reach end of file
                while (line != null)
                {
                    //write the lie to console window
                    Console.WriteLine(line);
                    //Read the next line
                    line = sr.ReadLine();
                }
 
                //close the file
                sr.Close();
                Console.ReadLine();
            }
            catch(Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
            finally
            {
                Console.WriteLine("Executing finally block.");
            }
        }
    }
}
 
//Write a text file////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.IO;
using System.Text;
 
namespace readwriteapp
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            
            Int64 x;
 
            try
            {
                //Open the File
                StreamWriter sw = new StreamWriter("C:\\Test.txt", true, Encoding.ASCII);
 
                //Writeout all the lines changing the coordinates.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
//
//                              HOW CAN I DO THIS???
//
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
                //close the file
                sw.Close();
            }
            catch(Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
            finally
            {
                Console.WriteLine("Executing finally block.");
            }
        }
    }
}

Answers (7)