Create/Modify Text File

HappyCode

New member
Joined
Apr 21, 2014
Messages
4
Programming Experience
Beginner
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...
C#:
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...
C#:
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 and the sample.txt file attached.

If you could give me a hand with this I would be extremely greatful!
Many thanks.
//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.");
            }
        }
    }
}
 
Last edited by a moderator:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,144
Location
Sydney, Australia
Programming Experience
10+
To copy the file and process the lines as you go, you can basically just do this:
using (var sourceFile = new StreamReader(sourceFilePath))
using (var destinationFile = new StreamWriter(destinationFilePath))
{
    string line;

    while (!sourceFile.EndOfStream)
    {
        line = sourceFile.ReadLine();

        // Process line here.

        destinationFile.WriteLine(line);
    }
}
To process the line containing the numbers you could do something like this:
var parts1 = line.Split(',');
var parts2 = parts1[0].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var x = Convert.ToDouble(parts2[0]);
var z = Convert.ToDouble(parts2[2]);

line = string.Format("{0} 0.0 {1}, {2}",
                     x,
                     Math.Sqrt(Math.Pow(x, 2) + Math.Pow(z, 2)),
                     parts1[1]);
You might have to fiddle with the whitespace a bit but hopefully you get the gist.
 

HappyCode

New member
Joined
Apr 21, 2014
Messages
4
Programming Experience
Beginner
Many thanks for your answer jmcilhinney!

I think your lines of code can do magic! I'm looking forward to get back to my desk (I'm on a trip now) to give it a go!

I will let you know how I am getting on with this.

Thanks again!
 
Last edited:

HappyCode

New member
Joined
Apr 21, 2014
Messages
4
Programming Experience
Beginner
Hello,

Many thanks for your answer Sjmcilhinney! Back from my tripI have given this a go and I have still a little problem regarding how to jump to specific lines of code. Please note that everything that contains # in the text
file is a comment and we don't mind loosing it. Also it is not important to keep the "tab spaces" (the text can be justified to the right). For this reason I have created an intermediate file with the text justified to the left to work
with it and with no comments to make it simpler. I forgot to say that sometimes we also have blank lines between the numbers will have ). So now we can start from there. Now the data I need to work with looks like that (as in interfile.txt):

[/code]
Blah blah blah...

point
[

//Sometimes you have an empty lines here and sometimes you don't.
-1.0
-1.0 1.0,
1.0 -1.0 1.0,
1.0 -1.0 -1.0,
//Same thing, sometimes we
can have empty line in between the lines containing the coordinates.
-1.0
-1.0 -1.0,
0.0 1.0 0.0
]

blah, blah, blah...

[/code]


I have tried the following:

[/code]
using (var
sourceFile = new StreamReader(interFile))
using (var destinationFile = new
StreamWriter(outputFile))
{
/////////////////////////////////////////////////////////////////////////////
HOW
CAN I WRITE THE OUTPUT FILE WITHOUT ANY BLANK LINE??? WE CAN LEAVE
THEM

/////////////////////////////////////////////////////////////////////////////
while
(!sourceFile.EndOfStream)
{
string line;
line =
sourceFile.ReadLine();

if (line.StartsWith("point"))

{

/////////////////////////////////////////////////////////////////////
how
can I skip the next two
lines??
/////////////////////////////////////////////////////////////////////

//This
will do the operations (I have tried this and works well)
var parts1 =
line.Split(',');
var parts2 = parts1[0].Split(new[] { ' ' },
StringSplitOptions.RemoveEmptyEntries);

// Transform points by doing
x,0,sqrt(y2+z2)
var x = Convert.ToDouble(parts2[0]);
var y =
Convert.ToDouble(parts2[1]);
var z = Convert.ToDouble(parts2[2]);
line =
string.Format("{0} 0.0 {1}, {2}", x, Math.Sqrt(Math.Pow(y, 2) + Math.Pow(z, 2)),
parts1[1]);
destinationFile.WriteLine(line);

/////////////////////////////////////////////////////////////////////////////////////////////////////
WHEN
WE FINISH WITH THE BLOCK OF NUMBERS WHE SHOULD STOP DOING THE
OPERATION
///////////////////////////////////////////////////////////////////////////////////////////////////////
}
else
{
destinationFile.WriteLine(line);
// This will write all the remaining lines
}
[/code]

So that is the story!


Any help would be more than welcome!!!
Many thanks!
 
Top Bottom