Hello,
I am very new to programming so please bare with me. I need to make a program that can read a .txt file and store each line in an array, so i can use some of its contents later (store in a database). Is this possible? and how would i go about doing that. I have tried with the streamreader and then making each line a list item, but then it wont let me convert to array. i would really like some help here, TNX!
Loading

Kirtan PatelRank #292 · 8.6K reputationPosted Oct 9, 2009, 6:54 AM EDT
private void button1_Click(object sender, EventArgs e)
{
string path = Environment.CurrentDirectory + @"\TextFile1.txt";
string[] allLines = File.ReadAllLines(path);
List<string[]> ArraysWithWords = new List<string[]>();
foreach (string str in allLines)
{
string[] Words = str.Split(' ');
ArraysWithWords.Add(Words);
}
//Show First Line's 3 Words in Text Box
int count = 0;
foreach (string str in ArraysWithWords[0])
{
if (count <3)
{
textBox1.Text += str;
}
count++;
}
}
Roei BarRank #1,271 · 1.4K reputationPosted Oct 8, 2009, 1:19 AM EDT
vin lantwert11 reputationPosted Oct 8, 2009, 2:40 AM EDT
This works partly, what happens now is each line is one item in the array, what i actually need is one array per line where the items in the array are the single words. is this possible?
Roei BarRank #1,271 · 1.4K reputationPosted Oct 8, 2009, 3:34 AM EDT
vin lantwert11 reputationPosted Oct 8, 2009, 8:19 AM EDT
When I try the above i get an error message saying cannot implicitly convert string[] to string. This is my code:
openFileDialog1.ShowDialog();
ChosenFile = openFileDialog1.FileName;
string[] readtext = File.ReadAllLines(ChosenFile, Encoding.UTF8);
string[] linesArray = new string[readtext.Length];
int counter = 0;
foreach (string line in readtext)
{
string[] _wordsArray = line.Split(' ');
linesArray[counter] = _wordsArray;
counter++;
}
Thanks for your help sofar
vin lantwert11 reputationPosted Oct 8, 2009, 11:26 PM EDT
string[] readtext = File.ReadAllLines(bestand, Encoding.UTF8);
textBox1.Text = readtext[1];
string regel1 = textBox1.Text;
string[] aRegel1 = regel1.Split(' ');
Now i have to use the textbox to convert it to the string, but this would mean that i have to make a textbox for every line i want in an array.
Kirtan PatelRank #292 · 8.6K reputationPosted Oct 9, 2009, 12:47 AM EDT
Just Give Me Two things
What you will Input and What you want as out with with Example I will Help you :)
vin lantwert11 reputationPosted Oct 9, 2009, 1:31 AM EDT
i have a text file that gets automatically generated by a website.
in this file there is a lot of data i need.
what i want is to read the file and have an array for each individual line
the text file size differs with each file.
is this enough info?
regards,
vin
Kirtan PatelRank #292 · 8.6K reputationPosted Oct 9, 2009, 3:39 AM EDT
Here i Coded .. if need some changes in it tell me :)
private void button1_Click(object sender, EventArgs e)
{
string path = Environment.CurrentDirectory+@"\TextFile1.txt";
string[] allLines = File.ReadAllLines(path);
List<string[]> ArraysWithWords = new List<string[]>();
foreach (string str in allLines)
{
string[] Words = str.Split(' ');
ArraysWithWords.Add(Words);
}
}
vin lantwert11 reputationPosted Oct 9, 2009, 5:30 AM EDT
bestand = openFileDialog1.FileName;
string[] allLines = File.ReadAllLines(bestand);
List
foreach (string str in allLines)
{
string[] Words = str.Split(' ');
ArraysWithWords.Add(Words);
}
textBox1.Text = ArraysWithWords[3];
}
the error i get: cannot implicitly convert type string[] to string.........
(sorry for the noobism)