需要从文件中读取一定数量的行到用逗号分隔的字符串中. [英] Need to read Cetain amount of lines from file into a string separated by comma's.

查看:95
本文介绍了需要从文件中读取一定数量的行到用逗号分隔的字符串中.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有以下代码,该代码可打开一个分页的分页文本文件,并在文件中每行发送一次短信.该文件如下所示:

手机#tab#消息
+12345678#tab#消息

只有手机号更改时,所有行上的消息都是相同的.

我使用的网站最多允许输入99个手机号码,并用逗号分隔.


因此,我要通过文件进行读取,获取第一批99个移动电话号码,并将其输入到{strmsg}中,并用逗号分隔.将其提交到站点,然后获取下一个99个号码,等等.

不太确定从哪里开始,一直在尝试这样的事情,确实开始让我知道包含手机号码的东西,但是我似乎最终陷入了循环,不知道下一步怎么做.只需寻求有关最佳方法的建议.

Hi I currently have the following code , which opens a tab delimted text file and sends out a sms per line in the file. The file looks as follows :

Mobile #tab# Message
+12345678 #tab# Message

The message is the same on all lines only the mobile number changes.

The site I use allows upto 99 mobile numbers to be entered separared by a comma.


So what I want to do it read through the file , get the 1st lot 99 mobile numbers and enter them into {strmsg} separated by comma"s.Submit that to the site and then get the next 99 numbers etc.

Not really sure where to start , have been trying somthing like this , which does start to get me the sting containing the mobile numbers , but then I seem to end up with a loop within a loop and don''t know what to do next.Just looking for some advice on the best way to do this.

Do While (objSR.Peek <> -1)

            strline = objSR.ReadLine
            fileline = strline.Split(vbTab)

            Dim num As String


            num = num + "," _
              & fileline(0)

        Loop






当前代码






Current Code

Dim objFS As New FileStream("c:\test.sms", FileMode.Open, FileAccess.Read)
        Dim objSR As New StreamReader(objFS)

        Dim strline, fileline() As String
        Do While (objSR.Peek <> -1)

            strline = objSR.ReadLine
            fileline = strline.Split(vbTab)

            Dim strnum As String = fileline(0)
            Dim strmsg As String = fileline(1)

            Dim request As HttpWebRequest
            Dim response As HttpWebResponse = Nothing

            Dim url As String
            Dim host As String = "http://southafrica.msg91.com"
            Dim senderid As String = "Test"

            Try

                url = host + "/sendhttp.php?" _
                         & "user=" & HttpUtility.UrlEncode(user) _
                         & "&password=" + HttpUtility.UrlEncode(password) _
                         & "&mobiles=" + HttpUtility.UrlEncode(strnum) _
                         & "&message=" + HttpUtility.UrlEncode(strmsg) _
                         & "&sender=" + HttpUtility.UrlEncode(senderid)



                request = DirectCast(WebRequest.Create(url), HttpWebRequest)

                response = DirectCast(request.GetResponse(), HttpWebResponse)

            Catch ex As Exception
                MsgBox(ex.Message)
            
        Loop

推荐答案

我希望我正确理解了您的问题.您当前的代码一一发送短信,而您想一一发送99短信.如果那是您的意图,那么您已经走了一半.您可以将发送代码更改为

I hope I understand your question right. Your current code sends the sms one by one, and you want to send 99 smss in one. If that is your intention you were half way down. You could change your sending code to

Dim objFS As New FileStream("c:\test.sms", FileMode.Open, FileAccess.Read)
Dim objSR As New StreamReader(objFS)
Dim counter As Int32 = 1
Dim num As System.Text.StringBuilder = New System.Text.StringBuilder(200)

Dim strline, fileline() As String
Do While (objSR.Peek <> -1)

    strline = objSR.ReadLine
    fileline = strline.Split(vbTab)

    Dim strnum As String = fileline(0)
    Dim strmsg As String = fileline(1)

    If counter > 1 Then
        num.Append(",")
    End If
    num.Append(strnum)

    If (counter Mod 99 = 0) OrElse (objSR.EndOfStream) Then
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing

        Dim url As String
        Dim host As String = "http://southafrica.msg91.com"
        Dim senderid As String = "Test"

        Try

            url = host + "/sendhttp.php?" _
                     & "user=" + HttpUtility.UrlEncode(user) _
                     & "&password=" + HttpUtility.UrlEncode(password) _
                     & "&mobiles=" + HttpUtility.UrlEncode(num.ToString()) _
                     & "&message=" + HttpUtility.UrlEncode(strmsg) _
                     & "&sender=" + HttpUtility.UrlEncode(senderid)



            request = DirectCast(WebRequest.Create(url), HttpWebRequest)

            response = DirectCast(request.GetResponse(), HttpWebResponse)

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        counter += 1
        num = New System.Text.StringBuilder(200)
    End If


Loop



我已使用mod语句将循环与当前代码集成在一起,以测试是否读取了99条消息,并使用StreamReader.EndOfStream来测试流的末尾.

我已经将"num as String"更改为"num as StringBuilder".如果要合并很多字符串值,则StringBuilder具有更好的性能(和内存使用情况).

我无法测试代码,因为它可能会发送数据.我希望它能正常工作.



I''ve intergrated your loop with your current code using the mod statement to test if 99 messages are read and the StreamReader.EndOfStream to test if the end of the stream is reached.

I''ve change the ''num as String'' to a ''num as StringBuilder''. If you are concatinating a lot of string values the StringBuilder has a beter preformence (and memory usage).

I couldn''t test the code as it might send data. I hope it will work.


文件很大吗?就像Wes所说的那样,您可以使用file.ReadAllLines并仅解析其中的99个.否则,您需要编写自己的代码来打开文件并一次读取一行.
Is the file huge ? Like Wes said you can use file.ReadAllLines and only parse 99 of them. Otherwise you need to write your own code to open the file and read one line at a time.


这篇关于需要从文件中读取一定数量的行到用逗号分隔的字符串中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆