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 PatelPosted Oct 9, 2009, 10:54 AM
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++;
}
}
vin lantwertPosted Oct 9, 2009, 9:30 AM
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)
Kirtan PatelPosted Oct 9, 2009, 7:39 AM
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 lantwertPosted Oct 9, 2009, 5:31 AM
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 PatelPosted Oct 9, 2009, 4:47 AM
Just Give Me Two things
What you will Input and What you want as out with with Example I will Help you :)
vin lantwertPosted Oct 9, 2009, 3:26 AM
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.
vin lantwertPosted Oct 8, 2009, 12:19 PM
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
Roei BarPosted Oct 8, 2009, 7:34 AM
vin lantwertPosted Oct 8, 2009, 6:40 AM
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 BarPosted Oct 8, 2009, 5:19 AM