Hi, I am new to C#. Please help me to solve this problem. Given an "input.txt", I want to obtain the "output.txt" as below:

input.txt
5,5
A(,)= 4.6;
B(,)= 0.1;
C(,)= 0.2;
D(,)= 1.6;
E(,)= 245;
F(,)= 3.3;
5,15
A(,)= 6.3;
B(,)= 0.9;
C(,)= 0.2;
D(,)= 1.6;
E(,)= 242;
F(,)= 2.9;
5,25
A(,)= 7.3;
B(,)= 0.2;
C(,)= 0.3;
D(,)= 1.5;
E(,)= 238;
F(,)= 1.9

output.txt
A(5,5)= 4.6;
B(5,5)= 0.1;
C(5,5)= 0.2;
D(5,5)= 1.6;
E(5,5)= 245;
F(5,5)= 3.3;
A(5,15)= 6.3;
B(5,15)= 0.9;
C(5,15)= 0.2;
D(5,15)= 1.6;
E(5,15)= 242;
F(5,15)= 2.9;
A(5,25)= 7.3;
B(5,25)= 0.2;
C(5,25)= 0.3;
D(5,25)= 1.5;
E(5,25)= 238;
F(5,25)= 1.9

From the input.txt, I want to replace the first six "(,)" with "(5,5)", the next six "(,)" with "(5,15)", and the last six "(,)" with "(5,25)". The problem I faced is how to loop 3 times and replace the (,) with "(5,5)", "(5,15)" and "(5,25)"?
In the meantime, I want to delete "5,5", "5,15" and "5,25" lines so that the output.txt will not have these lines.

using System.Text;
using System;
using System.IO;



namespace txt_Console_CSharp
{
    class Program
    {
        static void Main(string[] args)
        {

            String strFile = File.ReadAllText("c:\\input.txt");

            int ind = 0;
            int w, h;

            while (ind < 3)
            {

                w = 5;
                h = 5 + 10*(ind%4);

                for (int i = 0; i < 6; i++)    //loop "(,)" for six time     
                {
                    strFile = strFile.Replace("(,)", "("+w.ToString() + "," + h.ToString()+")");
                }

                ind++;
            }


            File.WriteAllText("d:\\output.txt", strFile);
        }
    }
}

Please advise.
Thank you.

Open input file for reading
Open output file for writing
Start a loop for 3 iterations
Read line from input (we'll call this sub)
Start a loop for 6 iterations
Read line from input (we'll call this base)
Use String.Replace on base replacing (,) with (sub) (we'll call this out)
Write out to output file
end loop (6)
end loop (3)
Close input file
Close output file

ssyzz, thanks for your reply.
However I am not sure what do you mean by:
Start a loop for 3 iterations
Read line from input (we'll call this sub)
Start a loop for 6 iterations
Read line from input (we'll call this base)

Can you please show me the code example in C#?

Thanks.

sch85 calls sub the lines starting with 5,5 5,15 etc.
Do your files always contain 3 of those "sub" items?

I was a little bit curious and i really wanted to do this since i haven t been using the streamreader and streamwritter classes for quite a time. Here is the code that solves your problem but only for 3 characters numbers if for example you had more you would have to make a method for more.

 static void Main(string[] args)
        {
            # region variables

            int counter=0;
            string replace= "";
            int counter2 = 0;
            string concatenate = "";
            int j=0;
            double[] array = new double[18]; // storing numbers; 
            char [] buffer=new char[10];
            string[] alphabet = new string[6] { "A", "B", "C", "D", "E", "F" };
            int interval = alphabet.Length;
            int num = 0;
            List<string> readnumber = new List<string>();

            #endregion

            List<char> items = new List<char>();
            // getting array results;
            try
            {
                using (StreamReader str = new StreamReader(@"....txt"))// file path
                {
                    while (!str.EndOfStream)
                    {
                        counter++;

                        if (counter%7==1) // for a longer data sheet
                        {
                           readnumber.Add(str.ReadLine());
                        }

                        else
                        {
                            str.Read(buffer, 0, buffer.Length); 
                            str.ReadLine();
                            for (int i = 0; i < buffer.Length; i++)
                            {
                                if (i > 5 && i < 9) // since there are only 3 characters (decimal, or int ) if there are more, code will not work
                                {
                                    counter2++;
                                    items.Add(buffer[i]);
                                    if (counter2 == 3)
                                    {
                                        foreach (char k in items)
                                        {
                                            concatenate = concatenate + k.ToString();
                                        }

                                        replace = concatenate.Replace('.', ',');
                                        array[j] = double.Parse(replace);
                                        j++;

                                        items.Clear();
                                        replace = "";
                                        concatenate = "";
                                        counter2 = 0;

                                    }
                                }
                            }
                        }
                    }
                }
            }

            // array filled whith result values;
            catch (Exception e) { Console.WriteLine(e.ToString()); }

            using (StreamWriter stw = new StreamWriter(@"C:\....txt")) 
            {
                while (counter2 < 3)
                {

                    for (int i = 0; i < alphabet.Length; i++)
                    {
                        stw.WriteLine(alphabet[i] + "({0})=" + array[num],readnumber[counter2]);
                        num++;
                    }

                    counter2++;
                }

            }

            Console.ReadLine();

        }

Hi dandebe, the above is just an example. Actually my program consists of 126 (i.e. from 5,5 ; 5,15 ; 5,25 ; until 5,1255) of those "sub" items.

Thank you castajiz for your code, however when I run it, I get the below result (shown below is a comparison of input.txt and output.txt):

**input.txt **
A(,)= 4.6;
B(,)= 0.1;
C(,)= 0.2;
D(,)= 1.6;
E(,)= 245;
F(,)= 3.3;

output.txt
A(5,5)=46
B(5,5)=1
C(5,5)=2
D(5,5)=16
E(5,5)=245
F(5,5)=33

The decimal point is lost from 4.6 to 46. Besides that 0.1 becomes 1. The semicolon sign ; also lost in the output.txt.
Please advise.

Thank you.

Are you paying him to write your code? No? Then make some effort on your own.

Do a File.ReadLines(), loop the lines and try parsing each line to a double value. If it succedes you have your text to fill up the lines until the following double exists.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.