Click here to Skip to main content
16,018,158 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
Hi All

Transmission Date : 06/12/12
Magazine # Items Item Dollars
AY 2 82.89
CK 8 210.48
SL 11 343.78
SU 5 104.00
TH 4 94.99
HA 1 21.40
EW 6 246.82
FO 2 179.00
IN 6 202.28
PE 102 5281.75
SI 7 332.20
SM 4 111.70
TD 10 375.49
EE 2 58.00
The totals for account : 2000027331828
Items: 170 Amount: $ 7644.78
*****************************************************
Magazine # Items Item Dollars
LV 7 162.00
The totals for account : 2000027331844
Items: 7 Amount: $ 162.00
*****************************************************


this is the notepad file. I need to read the Magazine,# Items,Item Dollars from the file and items count should be same and the amount count too.
Likewise there are some files in the folder.
This has to be populated to gridview.

Can anyone help me to do this efficiently.





Regards
Froxy
Posted
Comments
Andrei Straut 18-Sep-12 5:58am    
I don't believe anyone can help you do this efficiently until we see what you've tried. Try to do it yourself, and if you run into problems, then come back and ask. Posting a code snippet showing how you did it will help too.

It's pretty unlikely that someone will help unless you try to do it yourself. We are not here to do your work for you.
froxy 20-Sep-12 5:30am    
I have written this code

private void Form1_Load(object sender, EventArgs e)
{
bool IsHeader = false;
bool AllowRead = false;
DataTable dt = new DataTable();
dt.Columns.Add("Magazine");
dt.Columns.Add("#Items");
dt.Columns.Add("ItemDollars");
dt.Columns.Add("AccountNo");
dt.Columns.Add("PublisherId");


// Change this path to the directory you want to read
string path = "C:\\ACHImport\\";
DirectoryInfo dir = new DirectoryInfo(path);
foreach (FileInfo flInfo in dir.GetFiles())
{
String name = flInfo.Name;
long size = flInfo.Length;
DateTime creationTime = flInfo.CreationTime;
System.IO.StreamReader Reader = new
System.IO.StreamReader(path + name);
while (!Reader.EndOfStream)
{
DataRow dr;
int i = 0;
dr = dt.NewRow();
string StrHeader = Reader.ReadLine();
if (StrHeader.Contains("Magazine"))
{
AllowRead = true;
}
if (AllowRead)
{
string[] arrText;

arrText = StrHeader.Split(' ');
if (arrText.Length > 1)
{
int j = 0;

for (; i < arrText.Length; i++)
{
if (arrText[i] == "Magazine")
{
IsHeader = true;
break;
}
if (arrText[i] != string.Empty)
{
if (!StrHeader.Contains("The totals"))
{
dr[dt.Columns[j]] = arrText[i];
j++;
}
}
}
if(IsHeader)
dt.Rows.Add(dr);

}
string[] arrAccount;
arrAccount = StrHeader.Split(':');
if (arrAccount.Length > 1 && !arrAccount[0].ToLower().Contains("items"))
{
dt.Rows[i]["AccountNo"]=arrAccount[1];
}
string[] arrTotal;
arrTotal = StrHeader.Split(':');
if (arrTotal.Length > 1 && !arrTotal[0].ToString().ToLower().Contains("the totals for account"))
{
DataRow drTotal;
drTotal = dt.NewRow();
drTotal["#Items"] = arrTotal[2];
drTotal["ItemDollars"] = arrTotal[5];
dt.Rows.Add(drTotal);
}
}

}
Reader.Close();
}
}
froxy 20-Sep-12 5:31am    
Is there anyway to optimize the code?

1 solution

May this help you inspire:
C# CSV Reader and Writer[^]
 
Share this answer
 
Comments
froxy 20-Sep-12 5:31am    
Thanks for your comment.This is not csv file.

I have written like this

private void Form1_Load(object sender, EventArgs e)
{
bool IsHeader = false;
bool AllowRead = false;
DataTable dt = new DataTable();
dt.Columns.Add("Magazine");
dt.Columns.Add("#Items");
dt.Columns.Add("ItemDollars");
dt.Columns.Add("AccountNo");
dt.Columns.Add("PublisherId");


// Change this path to the directory you want to read
string path = "C:\\ACHImport\\";
DirectoryInfo dir = new DirectoryInfo(path);
foreach (FileInfo flInfo in dir.GetFiles())
{
String name = flInfo.Name;
long size = flInfo.Length;
DateTime creationTime = flInfo.CreationTime;
System.IO.StreamReader Reader = new
System.IO.StreamReader(path + name);
while (!Reader.EndOfStream)
{
DataRow dr;
int i = 0;
dr = dt.NewRow();
string StrHeader = Reader.ReadLine();
if (StrHeader.Contains("Magazine"))
{
AllowRead = true;
}
if (AllowRead)
{
string[] arrText;

arrText = StrHeader.Split(' ');
if (arrText.Length > 1)
{
int j = 0;

for (; i < arrText.Length; i++)
{
if (arrText[i] == "Magazine")
{
IsHeader = true;
break;
}
if (arrText[i] != string.Empty)
{
if (!StrHeader.Contains("The totals"))
{
dr[dt.Columns[j]] = arrText[i];
j++;
}
}
}
if(IsHeader)
dt.Rows.Add(dr);

}
string[] arrAccount;
arrAccount = StrHeader.Split(':');
if (arrAccount.Length > 1 && !arrAccount[0].ToLower().Contains("items"))
{
dt.Rows[i]["AccountNo"]=arrAccount[1];
}
string[] arrTotal;
arrTotal = StrHeader.Split(':');
if (arrTotal.Length > 1 && !arrTotal[0].ToString().ToLower().Contains("the totals for account"))
{
DataRow drTotal;
drTotal = dt.NewRow();
drTotal["#Items"] = arrTotal[2];
drTotal["ItemDollars"] = arrTotal[5];
dt.Rows.Add(drTotal);
}
}

}
Reader.Close();
}
}
froxy 20-Sep-12 5:31am    
Is there anyother way to optimize the code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900