| | |
Problem with arrays and list
Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
![]() |
•
•
Join Date: Mar 2008
Posts: 13
Reputation:
Solved Threads: 0
hey friends i am reading a csv file and i have to read a specifice coloumn form the file and that coloumn contains date and i have to read the date first and then keep a validation that if the dates in the file are between some dates then the program will read data from another file . The csv file contains a header and that creates a problem while reading the first row the file the file data is like this:
start_date_time,ani,dialed_digits,actual_dur,rounded_dur_secs,cost
"03/01/2008 00:05:57",629172162448,"923455755684",2,2,0.002800
"03/01/2008 00:15:56",79279906564,"79278454880",51,60,0.135000
"03/01/2008 00:16:51",4166143724,"92922202502",188,188,0.256900
"03/01/2008 00:23:13",07956563557,"925871021085",1020,1020,1.393400
"03/01/2008 00:38:13",639262060046,"923084230440",1,1,0.001400
"03/01/2008 00:47:02",--------------,"92945623075",124,124,0.169400
"03/01/2008 01:08:14",7187696383,"92946725834",457,457,0.624300
The code of mine goes here
start_date_time,ani,dialed_digits,actual_dur,rounded_dur_secs,cost
"03/01/2008 00:05:57",629172162448,"923455755684",2,2,0.002800
"03/01/2008 00:15:56",79279906564,"79278454880",51,60,0.135000
"03/01/2008 00:16:51",4166143724,"92922202502",188,188,0.256900
"03/01/2008 00:23:13",07956563557,"925871021085",1020,1020,1.393400
"03/01/2008 00:38:13",639262060046,"923084230440",1,1,0.001400
"03/01/2008 00:47:02",--------------,"92945623075",124,124,0.169400
"03/01/2008 01:08:14",7187696383,"92946725834",457,457,0.624300
The code of mine goes here
C# Syntax (Toggle Plain Text)
static void Main(string[] args) { StreamReader sr = new StreamReader(@"C:\Documents and Settings\jitendra\Desktop\ric_mar.csv"); StreamWriter sw = new StreamWriter(@"C:\dates.txt"); string dates = ""; string[] dvalue = null; IList<string> list = new List<string>(); while (!sr.EndOfStream) { dates = sr.ReadLine(); dvalue = dates.Split(',',';'); if (dvalue.Length > 3) { list.Add(dvalue[0].Replace("\"","")); } } DateTime begin = new DateTime(2008, 3, 1); DateTime end = new DateTime(2008, 3, 9); foreach (string var in list) { DateTime datevalue = DateTime.ParseExact(dvalue[0].ToString(), "3/1/2008 00:00:00", null); //DateTime datevalue = DateTime.Parse(var); Console.WriteLine(datevalue.ToString()); Console.ReadLine(); sw.WriteLine(var.ToString()); } sw.Flush(); sw.Close(); } } }
If you managed to read the date column data so put them in array
2- remove the ' " ' from each string(date) using string.Remove("\""); method
3- convert the string to date and compare the two dates DateTime.Compare(date1, date2);
4- based on value returns from DateTime.Comapre -1, 0 or 1 go through execution some codes...
2- remove the ' " ' from each string(date) using string.Remove("\""); method
3- convert the string to date and compare the two dates DateTime.Compare(date1, date2);
4- based on value returns from DateTime.Comapre -1, 0 or 1 go through execution some codes...
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Mar 2008
Posts: 13
Reputation:
Solved Threads: 0
hi ramy i have written the code like this have a look at it:
this prints the date in perfect manner now what i want is that if the array contains the date value between 03/01/2008 (mmddyyyy) to 03/09/2008 then the program should print the whole records which come in between these dates. so can you help me please..
C# Syntax (Toggle Plain Text)
StreamReader sr = new StreamReader(@"C:\Documents and Settings\jitendra\Desktop\Copy of ric_mar.csv"); StreamWriter sw = new StreamWriter(@"C:\dates.txt"); string dates = ""; string[] dvalue = null; IList<string> list = new List<string>(); IList<DateTime> mydd = new List<DateTime>(); while (!sr.EndOfStream) { dates = sr.ReadLine(); dvalue = dates.Split(',',';'); if (dvalue.Length > 3) { list.Add(dvalue[0].Replace("\"","")); } } foreach (string var in list) { DateTime datevalue = DateTime.ParseExact(var, "MM/dd/yyyy HH:mm:ss", null); sw.WriteLine(var.ToString()); }
Last edited by jitupatil_2007; Apr 5th, 2008 at 7:19 am.
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
Option #1
Option #2
Both examples create a new file with only the desired dates.
Option #1 simply copys the file into a new file where the date is in the desired range .
Option #2 is a little fancier, where it uses a DataTable to get the data, and allows you to write it out at one time. The datatable approach opens the door to all kinds of filtering, sorting, write out as XML, whatever you can do with a data table.
// Jerry
C# Syntax (Toggle Plain Text)
private void button2_Click(object sender, EventArgs e) { string sLine = ""; DateTime dates; DateTime start = Convert.ToDateTime("03/01/2008 00:00:00"); DateTime end = Convert.ToDateTime("03/09/2008 00:00:00"); StreamReader sr = new StreamReader(@"C:\cvsParse.txt"); StreamWriter sw = new StreamWriter(@"C:\dates.txt"); sw.WriteLine( sr.ReadLine() ); while (!sr.EndOfStream) { sLine = sr.ReadLine(); dates = Convert.ToDateTime(sLine.Replace("\"",string.Empty).Split(',')[0]); if (dates >= start && dates < end) sw.WriteLine(sLine); } sr.Close(); sw.Flush(); sw.Close(); }
Option #2
C# Syntax (Toggle Plain Text)
private void button1_Click(object sender, EventArgs e) { string header = ""; string dates = ""; string[] dvalue = null; StreamReader sr = new StreamReader(@"C:\cvsParse.txt"); StreamWriter sw = new StreamWriter(@"C:\dates.txt"); DataTable dt = new DataTable("cvs"); dt.Columns.Add("start_date_time", typeof(DateTime)); dt.Columns.Add("origin"); header = sr.ReadLine(); while (!sr.EndOfStream) { dates = sr.ReadLine(); dvalue = dates.Split(','); dt.Rows.Add(Convert.ToDateTime(dvalue[0].Replace("\"", string.Empty)) , dates ); } sr.Close(); DataRow[] dr = dt.Select("start_date_time > '03/01/2008 00:00:01' and start_date_time < '03/01/2008 00:30:59'"); sw.WriteLine(header); foreach (DataRow row in dr) sw.WriteLine(row["origin"].ToString()); sw.Flush(); sw.Close(); }
Both examples create a new file with only the desired dates.
Option #1 simply copys the file into a new file where the date is in the desired range .
Option #2 is a little fancier, where it uses a DataTable to get the data, and allows you to write it out at one time. The datatable approach opens the door to all kinds of filtering, sorting, write out as XML, whatever you can do with a data table.
// Jerry
![]() |
Similar Threads
- fstream Tutorial (C++)
- how to expand arrays (C++)
- Storing dynamic form values in Arrays for display & insert (PHP)
- Link List Sorting Problem (C++)
- Problem with a snippet of code in a shell program (C)
- Double Arrays HELP!!!!!! (C++)
- how to override arrays and objects??? (Java)
- arrays of pointers help (C)
- java problem with creating arrays (Java)
- passing arrays in visual basic (Visual Basic 4 / 5 / 6)
Other Threads in the C# Forum
- Previous Thread: Invalidate picturebox with rectangle on
- Next Thread: implementation of predictive parsing table
| Thread Tools | Search this Thread |
.net access algorithm animation array avltree bitmap box c# check checkbox client combobox control conversion csharp customactiondata database datagrid datagridview dataset date/time datetime datetimepicker degrees directrobot display dll draganddrop drawing encryption enum excel file filename files form format forms function gdi+ gtk hash image input install java keypress leak list math mouseclick mp3 mysql native networking operator outlook2003 packaging path photoshop picturebox pixelinversion pixelminversion post print process programming radians regex remoting richtextbox safari server sleep snooze socket sql statistics string table tables tcp text textbox thread time timer update usercontrol usercontrols validation visualstudio webbrowser webcam wfa wia winforms wpf xml






