Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, July 14, 2011 3:33 PM
Hi,
I am trying to create a new text file that is made up of text from 2 different txt. files in C#.
One text file has 20 Ip addresses and the other has 50 hostnames.
I'd like the new text file to match each 20 IP addresses to each of the 50 hostnames.
I should end up with new txt. file that contain 50 hostnames each with 20 Ip Addresses gotten from the first txt file.
The new file should contain 1000 entries with every hostname matching up with each of the 20 Ip Addresses.
All replies (16)
Friday, July 15, 2011 1:59 PM âś…Answered
Alright, you've put some effort in. I would suggest reading some C# tutorials though because right now you're trying to write the file object to the file, which just does a .ToString() on the file object and writes it (which is 'System.IO.StreamWriter').
string hostnamesFilePath = @"C:\2host.txt";
string ipsFilePath = @"C:\IPs.txt";
string outputFilePath = @"C:\next9.txt";
string[] hostnames = File.ReadAllLines(hostnamesFilePath);
string[] ips = File.ReadAllLines(ipsFilePath);
using (TextWriter textWriter = new StreamWriter(outputFilePath))
for (int i = 0; i < ips.Length; i++)
for (int j = 0; j < hostnames.Length; j++)
textWriter.WriteLine("{0}\t{1}", ips[i], hostnames[j]);
I just wrote this in the browser, so their may be minor errors, but that's the setup you want.
Thursday, July 14, 2011 3:37 PM
So what have you tried so far?
Thursday, July 14, 2011 4:02 PM | 2 votes
Hi,
what you have to do is:
1. read file 1 into stirng variable 1
2. read file 2 into string variable 2
3. put all variables together (3rd step is not needed, if you will combine both 1st two steps)
4. save the text into a new file
Example:
stirng path4 = @"C:\file1.txt";
stirng path4 = @"C:\file2.txt";
string newFilePath = @"C:\file3.txt";
stirng allText = System.IO.File.ReadAllText(path4);
allText+= "\r\n";
allText += System.IO.File.ReadAllText(path4);
using (FileStream fs = new FileStream(newFilePath, FileMode.OpenOrCreate))
{
System.IO.File.WriteAllText(newFilePath, allText);
}
This is the simpliest.
Mitja
Thursday, July 14, 2011 4:21 PM
Hi Mitja,
Thank you very much for your reply.
I was trying to use StreamReader without much luck in achieving what I needed.
Your code looks much more sensible.
I am getting the following error though.
Can you help?
The process can not access "C:\next3.txt" because it is being used by another process
usuing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
string path4 = @"C:\2host.txt";
string path4 = @"C:\IPs.txt";
string newFilePath = @"C:\next3.txt";
string allText = System.IO.File.ReadAllText(path4);
allText += "\r\n";
allText += System.IO.File.ReadAllText(path4);
using (FileStream fs = new FileStream(newFilePath, FileMode.OpenOrCreate))
{
System.IO.File.WriteAllText(newFilePath, allText);
}
}
}
}
The process can not access "C:\next3.txt" because it is being used by another process
Thursday, July 14, 2011 4:22 PM
I don't think that's quite what he's looking for. Probably something more like this:
string[] hostnames = File.ReadAllLines(pathToYourHostnamesFile);
string[] ips = File.ReadAllLines(pathToYourIpsFile);
// Nested loops and writing to the new file goes here
I'd like to see that he's tried something first though before just giving him the full code on how to do it.
Thursday, July 14, 2011 4:39 PM
Hi Scotty
Thanks for this.
Yes I tried using StreamReader but without luck.
I am new to C#.
I believe I have to convert the text of both files into an array and loop through to create the new file but I'm getting a bit lost.
Thursday, July 14, 2011 4:45 PM
string[] hostnames = File.ReadAllLines(pathToYourHostnamesFile);
string[] ips = File.ReadAllLines(pathToYourIpsFile);
using(/* Open file to write to here */)
for (int i = 0; i < hostnames.Length; i++)
for (int j = 0; j < ips.Length; j++)
// Write line to file here
Try that out, there are plenty of resources on how to write files all over the internet. Come back if you still have trouble.
Thursday, July 14, 2011 4:47 PM
cheers - will keep working on it - thanks for the steer!
Thursday, July 14, 2011 5:18 PM
Instead of this:
using (FileStream fs = new FileStream(newFilePath, FileMode.OpenOrCreate))
{
System.IO.File.WriteAllText(newFilePath, allText);
}
do:
//create file if not exist:
if (!File.Exists(newFilePath))
{
FileStream fs = File.Create(newFilePath);
fs.Close();
}
//now write to the created file
System.IO.File.WriteAllText(newFilePath, allText);
Mitja
Friday, July 15, 2011 9:03 AM
Hi Mitja,
Thanks for this. The fix enabled the files to be appended to each other but the end result I'm trying to get is a file that has each IP address matched to each host name
e.g
10.10.10.10 www.xxxxx.com
10.10.10.10 www.yyyyy.com
10.11.11.11 www.xxxxx.com
10.11.11.11 www.yyyyyy.com
I have tried to follow Scotty's advise using FileStream and StreamWriter but only get a new text file cretaed with the words "System.IO.StreamWrite" Can you suggest where I have gone wrong?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] hostnames = File.ReadAllLines(@"C:\2host.txt");
string[] ips = File.ReadAllLines(@"C:\IPs.txt");
{
FileStream aFile = new FileStream (@"C:\2host.txt", FileMode.Open);
FileStream bFile = new FileStream(@"C:\IPs.txt", FileMode.Open);
}
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\next9.txt"))
{
for (int i = 0; i < hostnames.Length; i++)
for (int j = 0; j < ips.Length; j++) ;
file.WriteLine(file);
Console.WriteLine(file);
}
}
}
}
Friday, July 15, 2011 2:34 PM
GW1948, do you really want to append for each IPAddress all the HostNames?
Isn`t this a bit weird, and pointeless? Becuase this is not how it is in real life (internet).
Mitja
Friday, July 15, 2011 2:37 PM
Hi Scotty,
Thanks for this, works a charm.
Will work on your advice!
Friday, July 15, 2011 2:39 PM
Piggy backing off Mitja, how about this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
String path4 = @"C:\file1.txt";
String path4 = @"C:\file2.txt";
String newFilePath = @"C:\file3.txt";
String text1 = String.Format("File1\r\n\r\n{0}", File.ReadAllText(path4));
text1 += "\r\n\r\nFile2\r\n\r\n";
text1 += File.ReadAllText(path4);
File.WriteAllText(newFilePath, text1);
}
}
}
John Grove, MCC - Senior Software Engineer
Friday, July 15, 2011 2:49 PM
Hi Mitja - it'll will be used for a host file to hit servers in a web farm individually
Friday, July 15, 2011 2:53 PM
Hi John, sorry a bit off topic, but since I have found you right here, did you loose any point here on msdn too? I lost 4000 of them. Isnt this strange?Mitja
Friday, July 15, 2011 2:55 PM
I rarely look at how many points I have so I am not sure. It does look like MSDN are in the process of implementing changes, so maybe things are in flux right now.John Grove, MCC - Senior Software Engineer