달력

42024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

function ReadFromTextFile (FileUrl,CharSet)
    dim str
    set stm=server.CreateObject("adodb.stream")
    stm.Type=2 'for text type
    stm.mode=3
    stm.charset=CharSet
    stm.open
    stm.loadfromfile FileUrl
    str=stm.readtext
 response.write str
    stm.Close
    set stm=nothing
    ReadFromTextFile=str
end function

 
Sub WriteToTextFile (FileUrl,byval Str,CharSet)       
    set stm=server.CreateObject("adodb.stream")
    stm.Type=2 'for text type
    stm.mode=3
    stm.charset=CharSet
    stm.open
    stm.WriteText str
    stm.SaveToFile fileurl,2   
    stm.flush
    stm.Close
    set stm=nothing
end sub

Readfile=readfromtextfile ("c:/11/readfilename.csv","utf-8")
writetotextfile "c:/11/writefilename.txt",Readfile,"utf-8"

 

일부 텍스트 파일은 한 줄 끝에 줄 넘기는 문자로 인해 텍스트 파일을 쓸 때 약간의 문제가 생길 수 있다

dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.CreateTextFile("c:/11/writefilename.txt",true)
f.write(readfile)
'f.write("How are you today?")
f.close
set f=nothing
set fs=nothing

을 사용하면 문제가 해결 될 수 있다

 

function은 리턴값이 ReadFromTextFile이 된다. 즉 함수명과 동일한 이름에 리턴값을 저장해야 한다.

sub는 리턴값이 없다. 단지 실행만 할뿐이다.

호출에서도 차이가 있다.

-----------------------------------------------------------------------------------

ASP에서 사용하는 Sub, Function

Sub는 단독 동작
Function은 동작하며 반환값을 돌려주는 녀석입니다.

Sub를 사용한 예제를 보면

Sub TestSub1()
  response.write("TestSub1성공"&"<br>")
End Sub
TestSub1()

Sub TestSub2(Astr)
  response.write("TestSub2성공"& Astr &"<br>")
End Sub
TestSub2("냠냠")

Sub TestSub3(Aint)
  response.write("TestSub3성공"& Aint+99 &"<br>")
End Sub
TestSub3(1)

Sub TestSub4(Aint,Bint)
  response.write("TestSub4성공"& Aint+Bint &"<br>")
End Sub
'TestSub4(1,99) ==> 이렇게 하면 에러납니다. *리턴값이 없는 Function도 똑같습니다.
TestSub4 1 , 99 '이렇게 하거나 또는
Call TestSub4(1 , 999) '이렇게 합니다. *리턴값이 없는 Function도 똑같습니다.


Function을 사용한 예제를 보세요

Function TestFunction2(Astr)
  TestFunction2 = "TestFunction2성공 - "& Astr &"<br>"
End Function
AAA=TestFunction2("냠냠")
response.write(AAA)


Function TestFunction3(Astr)
  TestFunction3 = "TestFunction3성공 - "& Astr + 99 &"<br>"
End Function
AAA=TestFunction3(1)
response.write(AAA)

Function TestFunction4(Aint,Bint)
  TestFunction4 = "TestFunction4성공 - "& Aint + Bint &"<br>"
End Function
AAA=TestFunction4(1,99)
response.write(AAA)


Function TestFunction5(Astr,Bstr)
  TestFunction5 = "TestFunction5성공 - "& Astr + Bstr &"<br>"
End Function
AAA=TestFunction5("냠냠","나나")
response.write(AAA)




출처:http://www.hoho31.com/hohoman/133

----------------------------------------------------------

일반 텍스트 파일 읽기

Dim fso
set fso = server.createObject("Scripting.FileSystemObject")''    일반 텍스트 파일 읽는 함수

if fso.FileExists(filePath) then
    set textStream = fso.OpenTextFile(filePath, 1, false,0)
    Dim contents

 Do While Not textStream.AtEndOfStream
 txtLine = textStream.ReadLine
 Response.Write txtline & "dd<br>"  '화면에 보여줄때 줄넘김을 위해 <br> 태그 추가
 Loop
    textStream.Close
    set textStream = nothing
else
     Response.write  "Error"
end if
set fso = nothing

'ASP' 카테고리의 다른 글

asp xml 파싱  (0) 2012.07.09
asp에서 웹페이지 소스 가져오기, 파싱  (0) 2012.06.29
ASP 날짜 처리 함수  (0) 2012.05.10
ASP 종료 함수  (0) 2012.04.05
ASP 문자열 함수  (0) 2012.04.02
Posted by dewlit
|