Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I am trying to read a huge .DAT file.I even have the csv of the .DAT file from which I wish to read specific columns eg column 1 and 2 and write the columns to a listbox. But while reading I was getting B�A.

This is my c# code

C#
private void btnLoad_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    string path = "";
    ofd.FileName = "";
    ofd.ShowDialog();
    path = ofd.FileName;
    string[] sp;
    if (string.IsNullOrEmpty(ofd.FileName))
    return;
    string[] lines= File.ReadAllLines(path);
    for (int i = 0; i < lines.Length; i++)
    {
        sp = lines[i].Split(',');
        listBox1.Items.Add(sp[0]);
    }

}

Please let me know if I am making a mistake in the above code and help me to rectify it.
Posted
Comments
Maciej Los 13-Oct-14 1:51am    
And the structure of data in a file is...
BillWoodruff 13-Oct-14 5:06am    
What is the specific error message you are getting ?

1 solution

I'd suggest to read this: Much ADO About Text Files[^]. This is very good article about reading/writing text files using ADO. Note that, that article is bit old and does not corresponds to .NET methods, although it's worth of reading.

In this case the best way - in my opinion - is to use ADO.NET OleDb[^]. Here is an article which shows 2 ways to import data from text file: Using OleDb to Import Text Files (tab, CSV, custom)[^].

Try!

[EDIT]
Please, see my past answer: how to import .dat file into datagrid using c#[^]

The next set of articles:
Reading comma delimited files[^]
Read Text File (txt, csv, log, tab, fixed length)[^]
[/EDIT]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 13-Oct-14 2:47am    
5ed.
—SA
Maciej Los 13-Oct-14 3:12am    
Thank you, Sergey ;)
vivek murli 13-Oct-14 3:17am    
I have updated my code

private void btnLoad_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
string path = "";
ofd.FileName = "";
ofd.ShowDialog();
path = ofd.FileName;
BinaryReader br = new BinaryReader(File.OpenRead(path));
foreach (char ch in br.ReadChars(5000))
richTextBox1.Text += ch;
//br.Dispose();
}

but I am getting characters like B�A. and squares in outpuy.What is to be done
Maciej Los 13-Oct-14 3:20am    
This code is not related to my answer... You did nothing what i mentioned.
vivek murli 13-Oct-14 3:24am    
But my Doubt was about reading a DAT file.I read the articles that you mentioned but not understanding anything

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