Click here to Skip to main content
15,069,133 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have text files with the below format:

89898 ABCF-ASDAS 23.88
45334 MJKI-OP MKLDFMK 89.88

How to read from the above format using flat file source connection? which parameters I can use to read the above formatted file? Help needed.
Posted
Updated 28-Sep-15 1:42am
v3

1 solution

Assuming the only time the space character can be used as the delimiter you can read your flat file one line at a time, then split by Space

C#
using (StreamReader sr = File.OpenText(Filepath)) //<-- Filepath here
                {
                    string line = String.Empty;
                    while ((line = sr.ReadLine()) != null)
                    {
     
                  
                        //split by SPACE
                        string[] values = line.Split(' ');
                MessageBox.Show(values[0]) // first time round would return 89898 
                MessageBox.Show(values[1]) // first time round would return ABCF-ASDAS
                MessageBox.Show(values[2]) // first time round would return 23.88      
  
                    }
   
v2
Comments
   
In some cases there are names with spaces in between them. For ex: ABCF ASDAS Instead of ABCF-ASDAS. What can I do for those values?
Member 12016701 28-Sep-15 8:14am
   
Is the 3rd value always a number? If so you could check if values[2] is a number...

using (StreamReader sr = File.OpenText(Filepath)) //<-- Filepath here
{
string line = String.Empty;
while ((line = sr.ReadLine()) != null)
{

//split by SPACE
string[] values = line.Split(' ');
decimal d;
bool isNumeric = Decimal.TryParse([values[2], out d);
if (isNumeric)
{
MessageBox.Show(values[0]);
MessageBox.Show(values[1]);
MessageBox.Show(values[2]);
}
else
{
MessageBox.Show(values[0]);
MessageBox.Show(values[1] + " " + values[2]);
MessageBox.Show(values[3]);
}
}
}

Not tested this yet

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