reading text file problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hamayun Khan
    New Member
    • Aug 2007
    • 106

    reading text file problem

    Hi All

    I have text files having queries like below.
    [code=sql]
    INSERT INTO tblJobScrap ([logoimage],[lea],[Region],[jobtitle],[ClosingDate],[PayScale],[Contract],[institutionName],[JobDesc],[extlink]) VALUES ('http://www.aberdeencit y.gov.uk/webapps/images/genericicons/acc-logo.gif','Aber deen','Scotland ','School Crossing Patroller','No fixed closing date','£6.0100 p.h.','£6.37-£6.65 p.h.','Permanen t','','http://www.aberdeencit y.gov.uk/webapps/jobs/jobDetails.asp? id=5591')[/code]

    [code=sql]
    INSERT INTO tblJobScrap ([logoimage],[lea],[Region],[jobtitle],[ClosingDate],[PayScale],[Contract],[institutionName],[JobDesc],[extlink]) VALUES ('http://www.aberdeencit y.gov.uk/webapps/images/genericicons/acc-logo.gif','Aber deen','Scotland ','Aquatics Teacher','No fixed closing date','£12.8900 p.h.','£11.03-£12.60 p.h.','R','','h ttp://www.aberdeencit y.gov.uk/webapps/jobs/jobDetails.asp? id=5595')
    [/code]

    I read into the file using asp.net code
    Code:
    <%
    Dim df As DirectoryInfo = New DirectoryInfo(Server.MapPath("~/ScrapJobs"))
                path = Server.MapPath("~/ScrapJobs/test.txt")
                fp = File.OpenText(path)
                sql = fp.ReadToEnd()
                'response.write(sql)            
                sql1 = Split(sql, "INSERT")
                fp.Close()
                fp = Nothing
                For j = 1 To UBound(sql1) 'loop through all the insert queries.
                    On Error Resume Next
                    ExecuteNonQry("INSERT " & sql1(j))
                Next
    %>
    This code read the text file successfully and insert queries are successfully executed but the £ signs are converted to ? marks. When I print the text using response.write I get the below text. On the browser the £(? mark) sign show like a box.

    [code=sql]
    INSERT INTO tblJobScrap ([logoimage],[lea],[Region],[jobtitle],[ClosingDate],[PayScale],[Contract],[institutionName],[JobDesc],[extlink]) VALUES ('http://www.aberdeencit y.gov.uk/webapps/images/genericicons/acc-logo.gif','Aber deen','Scotland ','School Crossing Patroller','No fixed closing date','?6.0100 p.h.','?6.37-?6.65 p.h.','Permanen t','','http://www.aberdeencit y.gov.uk/webapps/jobs/jobDetails.asp? id=5591')
    [/code]

    [code=sql]
    INSERT INTO tblJobScrap ([logoimage],[lea],[Region],[jobtitle],[ClosingDate],[PayScale],[Contract],[institutionName],[JobDesc],[extlink]) VALUES ('http://www.aberdeencit y.gov.uk/webapps/images/genericicons/acc-logo.gif','Aber deen','Scotland ','Aquatics Teacher','No fixed closing date','?12.8900 p.h.','?11.03-?12.60 p.h.','R','','h ttp://www.aberdeencit y.gov.uk/webapps/jobs/jobDetails.asp? id=5595')
    [/code]

    I don't know why £ sign are converted to ? marks.
    Waiting for quick response. Thanks in advance
    Last edited by Frinavale; Nov 5 '08, 03:12 PM. Reason: added code tags
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    You will have to specify encoding ...


    Code:
    FileStream file1 = new FileStream(@"C:\q3.txt", FileMode.Open);
                    byte[] buffer = new byte[file1.Length];
                    file1.Read(buffer, 0, buffer.Length);
                    string ss;               
    
                    ss = Encoding.Default.GetString(buffer);
    Console.WriteLine(ss);
    
    //q3.txt contains £12\n£12\n£12
    It will be better if you specify encoding while writing n reading files

    Code:
    string lines = @"£12\r\n£12\r\n£12";                 
                    System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\q3.txt", false, Encoding.UTF8);
                    file.WriteLine(lines);               
                    file.Close();
    
    
    //so while reading file u use this 
    ss = System.Text.Encoding.UTF8.GetString(buffer);

    Comment

    Working...