Search:
Namespaces
Discussions
.NET v1.1
Feedback
reading text into arrays
Messages
Related Types
This message was discovered on
microsoft.public.dotnet.languages.csharp
.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...
Christine (VIP)
I am working in C# visual studio. Right now I have managed to read in a text
file to an arraylist and write it to the console screen. What I would like
to do is convert this to a one dimensional array and write it to a new file.
My code is as below. The first chunk of it works, but when I try to create
the array, it writes System.
String
[] in the console.
Thanks
using System;
using System.IO;
using System.Collections;
namespace ReadWriteArrayCSharp
{
class Class1
{
static void Main(string[] args)
{
//opens streamreader to read in test1 file
StreamReader
objReader = new
StreamReader
("c:\\ChristineWork\\test1.txt");
string sLine="";
//reads file in as an arraylist
ArrayList arrText = new
ArrayList
();
//read until end of file
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
//add each line of text file in
arrText.Add(sLine);
}
objReader.Close();
foreach (string sOutput in arrText)
//write to console
Console.WriteLine(sOutput);
Console.ReadLine();
StreamWriter sw= new
StreamWriter
("c:\\ChristineWork\\testtest.txt");
// Create and initialize 1-D target array
Array final=
Array
.CreateInstance(typeof(string),96 );
//Copies entire arraylist to target array
arrText.CopyTo(final);
Console.Write(final);
sw.Write(final);
}
}
}
Reply to this message...
Dan
Christine,
1. The
ArrayList
structure is already 1 dimensional...
2. to write an
ArrayList
or string[] to file do the following:
StreamWriter
wtr =
File
.CreateText ( "myfilename.txt" );
wtr.AutoFlush = true; // good practice if you've lots of data
foreach ( string szLine in myArray )
{
wtr.WriteLine ( szLine );
}
wtr.Close();
"Christine" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Christine (VIP)
Ok that works, except that I would like to get it into 1 column of 96 rows.
My data i am working with are decimal numbers separated by spaces. How might
I go about this? Thank you so much for your help.
"Dan" wrote:
[Original message clipped]
Reply to this message...
Nicholas Paldino [.NET/C# MVP] (VIP)
Christine,
You might want to try using the Split method on the string class. It
will allow you to split a string up, delimiting it by whatever character
that you want. You can then get the parts in an array of strings, and print
them out however you wish.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
Click here to reveal e-mail address
"Christine" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Bob Grommes
// al is the
ArrayList
with the loaded file
foreach (string sOutput in al) {
// bool arg will supress empty values in case there are any
// values separated by more than one space.
string[] sFields = sOutput.Split(new char[] {' '},true);
foreach (string sField in sFields) {
// Write to the file.
}
}
This will give you 96 records from each of the original records.
String
.Split(char[],bool) will be obsoleted in CLR 2.0 by
String
.Split(char[],StringSplitOptions). The old overload will be left in
there for backward compatibility. CLR 2.0 won't offer any new functionality
(the StringSplitOptions enum only has members "None" and
"RemoveEmptyEntries") -- so I guess they did this for clarity and possibly,
to provide for additional options in the future.
--Bob
"Christine" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
> Ok that works, except that I would like to get it into 1 column of 96
rows.
> My data i am working with are decimal numbers separated by spaces. How
might
[Original message clipped]
Reply to this message...
Christine (VIP)
Bob,
I don't know if i interpreted your answer correctly and used it in my code
correctly. So it is posted below. I am getting an error that it cant access
the file to write to because it is being used by another process. Thanks!
using System;
using System.IO;
using System.Collections;
namespace ReadWriteArrayCSharp
{
class Class1
{
static void Main(string[] args)
{
//opens streamreader to read in test1 file
StreamReader objReader = new
StreamReader
("c:\\ChristineWork\\test1.txt");
string sLine="";
//reads file in as an arraylist
ArrayList arrText = new
ArrayList
();
//read until end of file
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
//add each line of text file in
arrText.Add(sLine);
}
objReader.Close();
foreach (string sOutput in arrText)
//write to console
Console.WriteLine(sOutput);
Console.ReadLine();
foreach (string sOutput in arrText)
{
// bool arg will supress empty values in case there are any
// values separated by more than one space.
string[] sFields = sOutput.Split(new char[] {' '});
foreach (string sField in sFields)
{
// Write to the file.
StreamWriter
sw= new
StreamWriter
("c:\\ChristineWork\\test11.txt");
sw.WriteLine(sField);
Console.WriteLine(sField);
}
Console.WriteLine();
}
}
}
}
"Bob Grommes" wrote:
[Original message clipped]
Reply to this message...
Jon Skeet [C# MVP] (VIP)
Christine <
Click here to reveal e-mail address
> wrote:
[Original message clipped]
Well, do you have another program using test11.txt? Try changing it to
a completely new filename and see if that helps.
--
Jon Skeet - <
Click here to reveal e-mail address
>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
Christine (VIP)
I'm sorry, I meant to include that. I have tried four or five different file
names and they have all given the same error.
"Jon Skeet [C# MVP]" wrote:
[Original message clipped]
Reply to this message...
Christine (VIP)
Again, I apologize, it was a simple mistake, (closing the streamwriter) and
I corrected it. Thank you!!
"Christine" wrote:
[Original message clipped]
Reply to this message...
System.Array
System.Collections.ArrayList
System.Console
System.IO.File
System.IO.StreamReader
System.IO.StreamWriter
System.String
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com