hareth py
hareth py

Reputation: 130

C# Append at last line a character on all then one by one?

I'm making console c# app that actually takes all lines from text1 and append to it in the end of each line a text that is ".php?" or ".html? and these texts are also lines from text2, I want to print the first one in text2 in the end of each line in text1. Then take the second one in text2 and do the same Until it finishes text2?

Here's my code:

string[] resultConfig = File.ReadAllLines("keywords.txt");
string[] readParameters = File.ReadAllLines("param.txt");
            for (int i = 0; i < readParameters.Length; i++)
            {
                for (int x = 0; x < resultConfig.Length ; x++)
                {
                    resultConfig[x] = resultConfig[x] + readParameters[i];
                    Console.WriteLine(resultConfig[x]);
   
                }
                
            }

OUTPUT: **

keyboards.php?.html?.asp?
karachi.php?.html?.asp?
keychain.php?.html?.asp?
in khobar.php?.html?.asp?
lebanon.php?.html?.asp?
lights.php?.html?.asp?
london.php?.html?.asp?
must have.php?.html?.asp?

**

**

WHAT IT SHOULD BE:
keyboards.php?
karachi.php?
keychain.php?
in khobar.php?
lebanon.php?
lights.php?
london.php?
must have.php?
keyboards.html?
karachi.html?
keychain.html?
in khobar.html?
lebanon.html?
lights.html?
london.html?
must have.html?

** etc...

** KEYWORDS.TXT CONTAINS **

keyboards
karachi
keychain
in khobar
lebanon
lights
london
must have

** PARAM.TXT CONTAINS **

.php?
.asp?
.html?

Upvotes: 0

Views: 269

Answers (3)

Rufus L
Rufus L

Reputation: 37020

It appears you want to save all these results in the resultConfig array, but you can't just add more items to an array than it was initialized with - you have to resize it first using Array.Resize(ref resultConfig, resultConfig.Length * readParameters.Length).

However, even then it will be a little tricky to append to the first set of items and then add new items for the additional parameters (it can be done if that's really necessary).

Instead I would create a new List<string> to save the results, and leave the initial arrays as they are:

string[] resultConfig = 
{
    "keyboards",
    "karachi",
    "keychain",
    "in khobar",
    "lebanon",
    "lights",
    "london",
    "must have"
};

string[] readParameters = {".php?", ".html?", ".asp?"};

var allCombinations = new List<string>();

for (int i = 0; i < readParameters.Length; i++)
{
    for (int x = 0; x < resultConfig.Length; x++)
    {
        allCombinations.Add(resultConfig[x] + readParameters[i]);
        Console.WriteLine(resultConfig[x] + readParameters[i]);
    }
}

Upvotes: 1

Tomsen
Tomsen

Reputation: 331

Your problem is this line resultConfig[x] = resultConfig[x] + readParameters[i];. In this line you change your string in resultConfig[x] and since you're using a nested loop, this happens for every line in your *param.txt` file.

In order to write you desired result in the console try this code instead:

            string[] resultConfig = File.ReadAllLines("keywords.txt");
            string[] readParameters = File.ReadAllLines("param.txt");
            for (int i = 0; i < readParameters.Length; i++)
            {
                for (int x = 0; x < resultConfig.Length ; x++)
                {
                    string line = resultConfig[x] + readParameters[i];
                    Console.WriteLine(line);
   
                }
                
            }

Upvotes: 1

H. de Jonge
H. de Jonge

Reputation: 890

You keep adding the parameter to the config and you should change the order of the loops and not change the value in the array.

Something like this:

string[] resultConfig = File.ReadAllLines("keywords.txt");
string[] readParameters = File.ReadAllLines("param.txt");

for (int x = 0; x < resultConfig.Length ; x++)
{
  for (int i = 0; i < readParameters.Length; i++)
  {
    Console.WriteLine(resultConfig[x] + readParameters[i]);
  }
}

Upvotes: 1

Related Questions