Writing to multiple files
Hi,
I want to read in a file which contains words ending with ';'.
And after that, i want to write the content of it to multiple files.
Depending on what the letters the word contains.
Suppose the word is 'reader'. Then i want it to cute and paste this word to a doc called "EA-words".
I declared and created the files. And i read in the document called "mixedwords.txt" and put its content to an arraylist.
This is as far as i got:
//declared 15 files to write to.
while((linesInArray = sr.ReadLine()) != null)
{
compareLines.Add(linesInArray);
}
Anyone any ideas
Thanks in advance!

Writing to multiple files
Manuel Sampen
Hi,
I got the elements in an arraylist so i can be able to sort them.
Want i want to do with the words in that array is to check if the word contains certain letters.
Suppose on index 3 is the word "contains". Then i want to put the word in a file called "ai-words.txt".
I have like 15 other cases like "ai-words.txt".
I just need one or two examples of how i can accomplish this.
Got any idead
seamonkeyz
*confused*
so you want to read a file which contains words ending with a semicolon ';'
so basically you want to store each word in a string[] array spliting at the ; If so:
StreamReader theReader = new StreamReader("file.txt");
string[] theWords = theReader.ReadToEnd().Split(new char[] {';'});
theReader.Close();
so now you have your words in the string[] array, seperated on each element by the semi colon. Now what do you want to do is this what you want to start of with
Sandra2006
Hi,
That helped very much!
Thank you!
Ima give it a try.
jaimlin
I'm too bored this night.
Check this:
string MyString;
int[] mrr = new int[1];
System.Collections.BitArray BA = new System.Collections.BitArray(5);
while(true)
{
MyString = Console.ReadLine();
if (MyString.Contains("a")) BA[0] = true;
if (MyString.Contains("e")) BA[1] = true;
if (MyString.Contains("i")) BA[2] = true;
if (MyString.Contains("o")) BA[3] = true;
if (MyString.Contains("u")) BA[4] = true;
BA.CopyTo(mrr, 0);
switch (mrr[0])
{
case 1 : // only contain 'a', etc.. break;
case 3 : // only contain 'a' and 'e' break;
case 5: // only contain 'a' and 'i' break;
// ... and etc.. etc.. etc.. etc... ;) default: { throw new ArgumentException("Unrecognized value");}
}
mrr[0] = 0;
BA.SetAll(false);
}
Posible values table:
a: 1
ae: 3
ai: 5
ao: 9
au: 17
e: 2
ea: 3
ei: 6
eo: 10
eu: 18
i: 4
ia: 5
ie: 6
io: 12
iu: 20
o: 8
oa: 9
oe: 10
oi: 12
ou: 24
u: 16
ua: 17
ue: 18
ui: 20
uo: 24
//... etc.. etc.. etc... to
aeiou: 31
// each combination has a different interger value
I hope that it will be usefull !!
Regards.
Nuno_Salvado
you can use the .Contains method to check if a string has a particular character/string in it.
foreach(object currentItem in theArrayList)
{
if (currentItem.ToString().Contains("word"))
{
//write to file the currentItem
}
}
You could also use regex, regular expressions to check if a string contains a series of patterns but be warned its tricky and can be expensive to use
suggestion: better of using a generic list as well, its type safe and that you know exactly what type of objects are contained in the collection.
List<string> theWords = new List<string>();
you can also sort the items too.
does this help
inthefields
;D
Ooi Kim Hong
Hi guys,
@ahmedilyas, its getting late, im not thinking clear anymore (LOL).
@V. Tortola, you should get bored more often :P
Thanks you guys, Ima look at it tomorrow!
Gnite!
re infecta
dont understand why you are writing and opening the file again when you have the words started in the collection
if the currentItem contains those 2 letters then it will return true :-)
l Bllizzd l
Hi,
I'm having a lil problem.
Something is wrong with my condition. Its writing everything to the document.
This is my code:
List<string> wordsInMixedDoc = new List<string>();
string linesInArray = null;
while ((linesInArray = sr.ReadLine()) != null)
{
wordsInMixedDoc.Add(linesInArray);
}
sr.Close();
foreach (string currentItem in wordsInMixedDoc)
{
using (StreamWriter writer = File.AppendText(@"C:\\testFolder\\oe.txt"))
{
if (currentItem.Contains("oe"))
{
using (StreamReader reader = File.OpenText("C:\\testFolder\\words.txt"))
{
writer.Write(reader.ReadToEnd() + "\r\n");
}
}
}
}
What am i doing wrong