CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2009
    Posts
    1

    Reading Strings from Text File problem

    I'm reading a text file and inserting it into database, using following code, which contains few strings like this

    DAV|FIN-ABC;0;0;363.03;|*
    DAV|SIN-CTF;0;0;299.46;|*
    DAV|POS-PTF;100;135;299.46;|*
    DAV|DON-ATF;200;150;363.03;|*
    DAV|DSS-ATF;100;135;297.46;|*

    All the values are inserted into table, problem is first column which is separated by 'pipe separator'. [FONT='Verdana','sans-serif']Can anyone amend the following code with liffle effort to separate first column with pipe and than others with semi colon which is working but problem is to separate first column with pipe separator.

    using
    System;
    using
    System.IO;
    using
    System.Collections.Generic;
    using
    System.Text;
    using
    System.Data;
    using
    System.Data.SqlClient;
    namespace
    Reading_File
    {
    class ReadingFile
    {
    static void Main(string[] args)
    {
    string str = string.Empty;
    string[] storage = null;
    string strClientBates = string.Empty;
    string strFilePath = string.Empty;
    char[] pipe ={ '|' };
    char[] semi ={ ';' };
    // string record_identifier;
    /* Variables to store values of Text FileTemporarily */
    string etf_symbol;
    string volume_traded;
    string etf_last_traded_price;
    string nav;
    /* Reading Text File for the values */
    using (FileStream file = new FileStream("Text.txt", FileMode.Open, FileAccess.ReadWrite))
    {
    using (StreamReader reader = new StreamReader(file))
    {
    while ((str = reader.ReadLine()) != null)
    {
    /* Separating values with 'semi-colon separator */
    storage = str.Split(semi);
    if (storage != null)
    {
    etf_symbol = storage[0];
    volume_traded = storage[1];
    etf_last_traded_price = storage[2];
    nav = storage[3];
    /* Opening Connection */
    string sConn = "server=SomeDatabase; uid=usergian; pwd=somepassword; database=hia; Connect Timeout=10000";
    SqlConnection ObjCon = new SqlConnection(sConn);
    /* Inserting values into Table */
    string sQuery = "Insert into etf values('','" + etf_symbol + "','" + volume_traded + "','" + etf_last_traded_price + "','" + nav + "')";
    SqlCommand ObjCmd = new SqlCommand(sQuery, ObjCon);
    ObjCon.Open();
    int a = ObjCmd.ExecuteNonQuery();
    ObjCon.Close();
    Console.WriteLine("Records inserted successfully");
    }
    }
    }
    }
    }
    }
    }

  2. #2
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Reading Strings from Text File problem

    please use code tags...

    and why don't you do it with regex?
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  3. #3
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: Reading Strings from Text File problem

    you can use the usual String-Separator method and put all string chunks into an array.
    It is just one line of code.

  4. #4
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Reading Strings from Text File problem

    either of these two will work....

    1. split on the semi-colon, then split the first element of that array by the pipe.
    2. split on the pipe, then split the second element of that array by the semi-colon.

    You have all of the code there. You just have to put it together.

  5. #5
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: Reading Strings from Text File problem

    look at this code, it is very simple.
    It takes a text line and splits the words into an array.
    The separator can hold many characters. So the split is done in only one line of code.


    Code:
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                char[]    sep = { '|', ';' };
                string[]  words;
                string    readTextLine = "DAV|FIN-ABC;0;0;363.03;|*";
    
                words = readTextLine.Split(sep);
    
    
                foreach (string singleWord in words)
                {
                    Console.WriteLine(singleWord);
                }
           }
        }
    }



    Console output will be:

    DAV
    FIN-ABC
    0
    0
    363.03
    *
    Last edited by MNovy; May 19th, 2009 at 06:48 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured