a3deluxe Posted August 5, 2013 at 04:24 PM Report #520886 Posted August 5, 2013 at 04:24 PM (edited) Boas, Precisava de uma ajudinha, Queria ler a primeira linha do ficheiro TXT e depois a 3 palavra (725540N) e a 5 palavra (470055N) No codigo seguinte já esta a ler a linha 1, só falta ler as palavras. Ficheiro TXT: 0N 728340N [b]725540N[/b] 470823N [b]470055N[/b] 2465291N 2218062N 2461770N 2216563N 735472547 2216563N2216563N OFF 87340578t8hr 2216563N Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim FILE_NAME As String = "c:\teste.txt" Dim TextLine As String If System.IO.File.Exists(FILE_NAME) = True Then Dim objReader As New System.IO.StreamReader(FILE_NAME) Do While objReader.Peek() <> -1 TextLine = TextLine & objReader.ReadLine() & vbNewLine Loop TextBox1.Text = TextLine TextBox2.Text = TextLine Else MsgBox("File Does Not Exist") End If End Sub Edited August 5, 2013 at 06:38 PM by thoga31 Formatação do tópico + GeSHi
nelsonr Posted August 5, 2013 at 04:36 PM Report #520887 Posted August 5, 2013 at 04:36 PM Boas, podes partir a string lida pelo caracter do espaço. Algo assim (não testado): Dim palavras As String() = TextLine.Split(" ") Depois deves conseguir aceder à terceira e quinta palavra acedendo ao array palavras. Convem é certificares-te que o array tem mais de 5 palavras antes de acederes.
a3deluxe Posted August 5, 2013 at 05:40 PM Author Report #520895 Posted August 5, 2013 at 05:40 PM boas nao consegui
nelsonr Posted August 5, 2013 at 07:45 PM Report #520904 Posted August 5, 2013 at 07:45 PM (edited) Não conseguiste porque... ? Aqui está um exemplo: Dim FILE_NAME As String = "c:\teste.txt" Dim TextLine As String = "" If System.IO.File.Exists(FILE_NAME) = True Then TextLine = System.IO.File.ReadAllText(FILE_NAME) Dim palavras As String() = TextLine.Split(" ") If palavras.Length > 4 Then TextBox1.Text = palavras(2) TextBox2.Text = palavras(4) End If Else MsgBox("File Does Not Exist") End If Edited August 5, 2013 at 07:46 PM by nelsonr
thoga31 Posted August 5, 2013 at 08:07 PM Report #520906 Posted August 5, 2013 at 08:07 PM (edited) Só um pequeno aparte, que a solução está bem encaminhada e pouco mais há a acrescentar. Porque é que tanta gente insiste em fazer a verificação de um Boolean numa estrutura If? O "= True" é completamente desnecessário e redundante, apenas enche a vista e o processador de coisas desnecessárias. 😉 ' Redundante: If System.IO.File.Exists(FILE_NAME) = True Then ' Mais correcto... If System.IO.File.Exists(FILE_NAME) Then If Not System.IO.File.Exists(FILE_NAME) Then Edited August 5, 2013 at 08:08 PM by thoga31 Knowledge is free!
nelsonr Posted August 5, 2013 at 08:50 PM Report #520910 Posted August 5, 2013 at 08:50 PM Eu não uso, apenas utilizei o exemplo do OP. No entanto há que prefira muitas vezes soluções redundantes para facilitar a interpretação. Principalmente para quem não está habituado à linguagem.
a3deluxe Posted August 6, 2013 at 05:31 AM Author Report #520927 Posted August 6, 2013 at 05:31 AM (edited) Boas, Tentei assim e não apareceu nada nas TextBoxs, tambem não tem erros, // Dim FILE_NAME As String = "c:\teste.txt" Dim TextLine As String = "" Dim palavras As String() = TextLine.Split(" ") Dim objReader As New System.IO.StreamReader(FILE_NAME) If System.IO.File.Exists(FILE_NAME) Then If Not System.IO.File.Exists(FILE_NAME) Then TextLine = System.IO.File.ReadAllText(FILE_NAME) Do While objReader.Peek() <> -1 TextLine = TextLine & objReader.ReadLine() & vbNewLine Loop If palavras.Length > 10 Then TextBox1.Text = palavras(7) TextBox2.Text = palavras(9) End If Else MsgBox("File Does Not Exist") End If End If End Sub Edited August 6, 2013 at 09:57 AM by ribeiro55
nelsonr Posted August 6, 2013 at 08:32 AM Report #520932 Posted August 6, 2013 at 08:32 AM (edited) Boas, grandes trocas que andaste a fazer. então estás a dividir a TextLine logo apos a definição, que está vazia. Dim TextLine As String = "" Dim palavras As String() = TextLine.Split(" ") Estás a verificar se o ficheiro existe e se existir, estás a verificar se não existe? If System.IO.File.Exists(FILE_NAME) Then If Not System.IO.File.Exists(FILE_NAME) Then Estás a ler o ficheiro duas vezes. O ReadAllText já lê o conteúdo todo do ficheiro para a variavel, não precisas do restante TextLine = System.IO.File.ReadAllText(FILE_NAME) Do While objReader.Peek() <> -1 TextLine = TextLine & objReader.ReadLine() & vbNewLine Loop Experimentaste o código que coloquei lá em cima? Não precisavas de alterar nada Edited August 6, 2013 at 08:33 AM by nelsonr
a3deluxe Posted August 6, 2013 at 10:40 AM Author Report #520942 Posted August 6, 2013 at 10:40 AM Mais uma vez obrigado nelsonr Já funcionou. Agora so tenho que acertar as palavras. // If palavras.Length > 10 Then isto corresponde ao tamanho das palavras?
nelsonr Posted August 6, 2013 at 10:44 AM Report #520943 Posted August 6, 2013 at 10:44 AM Agora so tenho que acertar as palavras. // If palavras.Length > 10 Then isto corresponde ao tamanho das palavras? Corresponde ao tamanho do array palavras (quantos elementos tem), que por sua vez será o número de palavras que está no ficheiro
a3deluxe Posted August 6, 2013 at 11:00 AM Author Report #520945 Posted August 6, 2013 at 11:00 AM ao fazer a leitura do ficheiro txt esta palavra (725540N) tem um N junto dos numeros, há como ler so os numeros, e não aparecer o N??
nelsonr Posted August 6, 2013 at 11:03 AM Report #520946 Posted August 6, 2013 at 11:03 AM (edited) Se for sempre N, podes remover usando por exemplo: TextBox1.Text = palavras(7).Replace("N","") Edited August 6, 2013 at 11:03 AM by nelsonr
a3deluxe Posted August 6, 2013 at 12:50 PM Author Report #520953 Posted August 6, 2013 at 12:50 PM mais uma situação resolvida, Obrigado ja agora como posso abrir um ficheiro sem exteçao, em vez do TXT, é um ficheiro que abro com o Notpad
nelsonr Posted August 6, 2013 at 01:12 PM Report #520954 Posted August 6, 2013 at 01:12 PM Abrir onde? pela aplicação? Abres da mesma forma. No nome do ficheiro não colocas extensão.
a3deluxe Posted August 6, 2013 at 01:27 PM Author Report #520959 Posted August 6, 2013 at 01:27 PM sim na aplicação. já fiz isso e nao funcionou. não coloquei a extensao. depois testei com o ficheiro .txt e já funcionou.
nelsonr Posted August 6, 2013 at 01:40 PM Report #520962 Posted August 6, 2013 at 01:40 PM A extensão do ficheiro não devia afectar em nada. O que é que não funcionou exactamente?
a3deluxe Posted August 6, 2013 at 04:51 PM Author Report #521004 Posted August 6, 2013 at 04:51 PM nao fez a leitura das palavras, as textboxs ficaram em branco. não dá erro de não encontrar o ficheiro.
nelsonr Posted August 6, 2013 at 04:52 PM Report #521005 Posted August 6, 2013 at 04:52 PM Faz debug e vê onde está o problema. Verifica se o File.Exists está a verificar o ficheiro correctamente
a3deluxe Posted August 6, 2013 at 05:34 PM Author Report #521006 Posted August 6, 2013 at 05:34 PM (edited) esta a dar erro aqui If System.IO.File.Exists(FILE_NAME) = True Then mas ja veriquei e o ficheiro existe Edited August 7, 2013 at 12:08 AM by thoga31 Correcção da linguagem (GeSHi)
a3deluxe Posted August 6, 2013 at 06:57 PM Author Report #521009 Posted August 6, 2013 at 06:57 PM (edited) Testei com o primeiro codigo que postei aqui no topico e funcionou. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim FILE_NAME As String = "d:\Teste\Teste" Dim TextLine As String If System.IO.File.Exists(FILE_NAME) = True Then Dim objReader As New System.IO.StreamReader(FILE_NAME) Do While objReader.Peek() <> -1 TextLine = TextLine & objReader.ReadLine() & vbNewLine Loop TextBox1.Text = TextLine TextBox2.Text = TextLine Else MsgBox("File Does Not Exist") End If End Sub Com este codigo ele funciona, não precisa da extensao. Edited August 7, 2013 at 12:09 AM by thoga31 Correcção da linguagem (GeSHi)
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now