June 11th, 2010 10:21 AM
#1
[RESOLVED] Print out array contents into a text file
How would i go about printing out the contents of an array into a text file.
I know how to print single lines into a text file but not the contents of an entire array.
Any help is greatly appreciated.
June 14th, 2010 02:20 AM
#2
Re: Print out array contents into a text file
By "print to text file" do you mean to save the text into the file? Try look here .
Make it run.
Make it right.
Make it fast.
Don't hesitate to rate my post.
June 14th, 2010 08:06 AM
#3
Re: Print out array contents into a text file
Yes, I would want to print each element of the array as a different line in a text file.
i.e--- array lines = {"Each item","in this","array will","be printed out"," as a different line"}
so in the text file this would print out to....each item would be a different line in it...
could it be as simple as like a foreach loop for each item in the array?
June 14th, 2010 08:20 AM
#4
Re: Print out array contents into a text file
The easiest way (I think) is to create a string and use the System.IO.File.WriteAllText method:
Code:
string contents = "";
foreach(string line in lines)
contents += line + Environment.Newline;
File.WriteAllText(path, s, Encoding.Default);
It's not a bug, it's a feature!
June 14th, 2010 08:37 AM
#5
Re: Print out array contents into a text file
Thanks!
Its actually a little easier this way i figured out:
string[] lines = {"each","word","is","a","different","line"};
File.WriteAllLines(@"c:\ttt.txt", lines, Encoding.Default);
you dont even need a foreach loop just "File.WriteAllLines" and then the name of the array
June 14th, 2010 08:51 AM
#6
Re: [RESOLVED] Print out array contents into a text file
You're right I'm just not used to having my data arranged in an array beforehand
It's not a bug, it's a feature!
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks