Click here to Skip to main content
15,174,500 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]
   
v2
Comments
   
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
Maciej Los 13-Oct-14 3:31am
   
As per i understand your dat file is not binary file at all. It's text file, but with *.dat extension. So, you can use ADO.NET to read it.
Please, see updated answer.
CPallini 13-Oct-14 3:46am
   
5.
Maciej Los 13-Oct-14 3:48am
   
Thank you, Carlo ;)
BillWoodruff 13-Oct-14 5:04am
   
+5 Great answer.
Maciej Los 13-Oct-14 5:13am
   
Thank you, Bill ;)
Do you have an account on LinkedIn? If yes, please provide me a link. I'd like to invite you ;)
BillWoodruff 13-Oct-14 5:37am
   
Hi Maciej, I am an old hermit in this incarnation :) ... don't use any "social networks" except this one. I appreciate the invitation ! cheers, Bill
Maciej Los 13-Oct-14 5:45am
   
There is a lot old hermits on LinkedIn, including me ;) If you ever decide to create account on LinkedIn, let me know.
Cheers,
Maciej

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