Everything,
I am trying to write a test program that will eventually allow me to take the CSV.txt file and put its contents into the SQLite database file. There are hundreds of lines in TXT files that I work with.
Here's what I still have: a WPF form with three buttons -
Output: self-evident
Download CSV file: The "Open Win32 file" dialog box opens.
Current directory information: finds the current directory from which the program runs and displays it.
and a text block that displays various information from the program output - basically, regardless of what I want, things like exceptions, etc.
To read a text file, I implement the following code:
using System; using System.Data; using System.Data.SQLite; using System.IO; using System.Windows; using System.Windows.Controls; namespace C_Sharp_SQLite_Testbed { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { private static string fileName; public MainWindow() { InitializeComponent(); } private void btnLoad_Click(object sender, RoutedEventArgs e) { Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.FileName = "Document"; dlg.DefaultExt = ".txt"; dlg.Filter = "Text Documents (.txt)|*.txt"; Nullable<bool> result = dlg.ShowDialog(); if (result == true) { fileName = dlg.FileName; OutputConsole.Text = " "; OutputConsole.Text = fileName; try { using (StreamReader sr = new StreamReader(fileName)) { while (!sr.EndOfStream) { var line = sr.ReadLine(); var lineWords = line.Split(','); OutputConsole.Text = Convert.ToString(lineWords.Length); } } } catch (Exception ex) { OutputConsole.Text = " "; OutputConsole.Text = ex.Message; } } } private void btnExit_Click(object sender, RoutedEventArgs e) { Environment.Exit(0); } private void btnInfo_Click(object sender, RoutedEventArgs e) { Environment.CurrentDirectory = Environment.GetEnvironmentVariable("windir"); DirectoryInfo info = new DirectoryInfo("."); lock (info) { OutputConsole.Text = String.Format("Directory info: " + info.FullName); } } } }
This is a fairly simple setup, and the only method I'm having problems with is btnLoad_Click . The StreamReader method receives the StreamReader variable from the dialog box as an argument. It successfully opens the file. Until at the end of the file stream, the line is read using var line = sr.ReadLine(); . Then we split the string into an array with var lineWords = line.Split(','); . After that, the length of the linesWords array is printed in the text block using OutputConsole.Text = Convert.ToString(lienWords.Length); . The problem is this: it would seem that only one value is read from the file, the reader stops at the first comma, and then stops reading. The printable value of the array is 1, and when you change OutputConsole.Text = Convert.ToString(lineWords.Length) to OutputConsole.Text = Convert.ToString(lineWords[1]); only one value is printed. Also, this is not the correct value. So basically, I only have one place in the array, and it is not filled with the correct value.
What do you propose to change? Reading files in C # has never been my strongest.
EDIT: here are the first two lines as a sampler of what is in the file. This is the output of Kinect.
Time,HLState,HLX,HLY,HLZ,KLState,KLX,KLY,KLZ,ALState,ALX,ALY,ALZ,FLState,FLX,FLY,FLZ,HRState,HRX,HRY,HRZ,KRState,KRX,KRY,KRZ,ARState,ARX,ARY,ARZ,FRState,FRX,FRY,FRZ,lknfx,lknvg,rknfx,rknvg
These are just the headlines.
Second line: 700449555,2,-0.2912986,-0.1036692,1.472573,2,-0.2512482,-0.472762,1.416523,1,-0.2034467,-0.9132867,1.340637,1,-0.1271965,-0.9447169,1.280763,2,-0.197726,-0.09682589,1.596856,2,-0.1457276,-0.5412285,1.782268,2,-0.184881,-0.5280698,1.408923,2,-0.1630141,-0.537811,1.523656,178.867138094441,12.3859203137083,64.9231529324685,142.847159325228
I believe the encoding is ANSI.
EDIT:
Here is the "new" block for reading lines. It still works well, now you need to split things into a 2D array.
try { using (StreamReader sr = new StreamReader(fileName)) { lines = File.ReadAllLines(fileName); } int numberLines = lines.Length; OutputConsole.Text = " "; OutputConsole.Text += numberLines + " "; for (int i = 0; i < numberLines; i++) { OutputConsole.Text += lines[i] + "\n"; } }