By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,214 Members | 1,956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,214 IT Pros & Developers. It's quick & easy.

reading text file problem

100+
P: 106
Hi All

I have text files having queries like below.
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tblJobScrap ([logoimage],[lea],[Region],[jobtitle],[ClosingDate],[PayScale],[Contract],[institutionName],[JobDesc],[extlink]) VALUES ('http://www.aberdeencity.gov.uk/webapps/images/genericicons/acc-logo.gif','Aberdeen','Scotland','School Crossing Patroller','No fixed closing date','6.0100 p.h.','6.37-6.65 p.h.','Permanent','','http://www.aberdeencity.gov.uk/webapps/jobs/jobDetails.asp?id=5591')
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tblJobScrap ([logoimage],[lea],[Region],[jobtitle],[ClosingDate],[PayScale],[Contract],[institutionName],[JobDesc],[extlink]) VALUES ('http://www.aberdeencity.gov.uk/webapps/images/genericicons/acc-logo.gif','Aberdeen','Scotland','Aquatics Teacher','No fixed closing date','12.8900 p.h.','11.03-12.60 p.h.','R','','http://www.aberdeencity.gov.uk/webapps/jobs/jobDetails.asp?id=5595')
  2.  
I read into the file using asp.net code
Expand|Select|Wrap|Line Numbers
  1. <%
  2. Dim df As DirectoryInfo = New DirectoryInfo(Server.MapPath("~/ScrapJobs"))
  3.             path = Server.MapPath("~/ScrapJobs/test.txt")
  4.             fp = File.OpenText(path)
  5.             sql = fp.ReadToEnd()
  6.             'response.write(sql)            
  7.             sql1 = Split(sql, "INSERT")
  8.             fp.Close()
  9.             fp = Nothing
  10.             For j = 1 To UBound(sql1) 'loop through all the insert queries.
  11.                 On Error Resume Next
  12.                 ExecuteNonQry("INSERT " & sql1(j))
  13.             Next
  14. %>
  15.  
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.

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tblJobScrap ([logoimage],[lea],[Region],[jobtitle],[ClosingDate],[PayScale],[Contract],[institutionName],[JobDesc],[extlink]) VALUES ('http://www.aberdeencity.gov.uk/webapps/images/genericicons/acc-logo.gif','Aberdeen','Scotland','School Crossing Patroller','No fixed closing date','?6.0100 p.h.','?6.37-?6.65 p.h.','Permanent','','http://www.aberdeencity.gov.uk/webapps/jobs/jobDetails.asp?id=5591') 
  2.  
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tblJobScrap ([logoimage],[lea],[Region],[jobtitle],[ClosingDate],[PayScale],[Contract],[institutionName],[JobDesc],[extlink]) VALUES ('http://www.aberdeencity.gov.uk/webapps/images/genericicons/acc-logo.gif','Aberdeen','Scotland','Aquatics Teacher','No fixed closing date','?12.8900 p.h.','?11.03-?12.60 p.h.','R','','http://www.aberdeencity.gov.uk/webapps/jobs/jobDetails.asp?id=5595')
  2.  
I don't know why sign are converted to ? marks.
Waiting for quick response. Thanks in advance
Oct 28 '08 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 750
PRR
You will have to specify encoding ...


Expand|Select|Wrap|Line Numbers
  1. FileStream file1 = new FileStream(@"C:\q3.txt", FileMode.Open);
  2.                 byte[] buffer = new byte[file1.Length];
  3.                 file1.Read(buffer, 0, buffer.Length);
  4.                 string ss;               
  5.  
  6.                 ss = Encoding.Default.GetString(buffer);
  7. Console.WriteLine(ss);
  8.  
  9. //q3.txt contains 12\n12\n12
  10.  
  11.  
  12.  
It will be better if you specify encoding while writing n reading files

Expand|Select|Wrap|Line Numbers
  1.  
  2. string lines = @"12\r\n12\r\n12";                 
  3.                 System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\q3.txt", false, Encoding.UTF8);
  4.                 file.WriteLine(lines);               
  5.                 file.Close();
  6.  
  7.  
  8. //so while reading file u use this 
  9. ss = System.Text.Encoding.UTF8.GetString(buffer); 
  10.  
  11.  
Nov 5 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.