|
hello -
am trying to open and/or read a large csv file in VB, then write it to a txt file, starting with the 2nd line. i can open the csv, but cant seem to read (getting "past end of file" error) or write it. can manually open it in notepad and save it, then open/read THAT file, but defeats the purpose.... ideas, suggestions? thnx!
dubscat
|
|
|
|
|
Post some code and the folks here will be able to help you out.
|
|
|
|
|
sure, thanks -
been trying to approach this a couple of different ways:
FIRST -- am getting "input past end of file" error at the "split()" line, which i cant seemt to resolve:
....
BCMcsv = Dir$(BCMdrive & BCMtext)
Set objFSO = CreateObject("scripting.filesystemobject")
Set objCSV = objFSO.CreateTextFile(BCMdrive & BCMcsv, True, False)
Set objTXT = objFSO.OpenTextFile(BCMdrive & BCMcsv, 1)
sLines = Split(objTXT.ReadAll, vbCvLf)
objTXT.Close
iNumberOfLines = UBound(sLines)
If iNumberOfLines > 1 Then
Set objTXT = objFSO.OpenTextFile(objCSV, 2)
For i = 1 To iNumberOfLines
objTXT.WriteLine sLines(i)
Next
objTXT.Close
End If
...Set objects = nothing ...
SECOND -- blows up at the first line input:
...
BCMcsv = Dir$(BCMdrive & BCMtext)
Dim f1 As Long
Dim f2 As Long
Dim strLine As String
f1 = FreeFile
Open BCMdrive & BCMcsv For Input As #f1
f2 = FreeFile
Open BCMdrive & "tempBCM.txt" For Output As #f2
Line Input #f1, strLine
Do While Not EOF(1)
Line Input #f1, strLine
Print #f2, strLine
Loop
Close #f1
Close #f2
...
|
|
|
|
|
I wrote this just now and it seems to work fine.
Give it a try ...
Dim sr As System.IO.StreamReader
Dim sw As System.IO.StreamWriter
Dim sLine As String
Dim iCount As Integer = 0
sr = New System.IO.StreamReader("C:\temp\file1.txt")
sw = New System.IO.StreamWriter("C:\temp\file2.txt")
sLine = sr.ReadLine()
While Not (sLine Is Nothing)
iCount = iCount + 1
If (iCount > 1) Then
sw.WriteLine(sLine)
End If
sLine = sr.ReadLine()
End While
sr.Close()
sr.Dispose()
sw.Close()
sw.Dispose()
|
|
|
|
|
thanks dave -
i'm getting a "compile error: type mismatch" error at the
sr = New System.IO.StreamReader(mydrive & myfile)
line. also, the only way it seems to take without showing red text in the module is to delete the "New" i'm on excel 2003 but am not seeing any project references that would seem to apply here. please advise, thnx!
|
|
|
|
|
Sorry my example was written in Visual Studio 2010 VB.NET
I did not realize you were looking for a solution in Excel VBA.
If I get time, I'll try to post an example in Excel VBA. I should have picked up on it when you posted your code sample.
My bad.
|
|
|
|
|
no worries, dave - i appreciate your help here, thnx!
|
|
|
|
|
...yes, did research this a little and discovered this was a .net contstuct. we dont have that available here, otherwise would have been happy to explore it.
|
|
|
|
|
got it, dave -
final code is below; thnx for your help!
|
|
|
|
|
Great to know that you solved your problem.
The folks here are very helpful.
|
|
|
|
|
From your description, I think the lines in your CSV file are not delimited by CRLF, but only LF. That would explain why you are able to open / save in Notepad, and use the file.
HTH
Happiness will never come to those who fail to appreciate what they already have. -Anon
|
|
|
|
|
thanks chandraram -
thought i had tried that and didnt work either. will try it again now...
|
|
|
|
|
thanks again, ChandraRam -
did try to work with the LF specification, and that is how the csv is delimited, however still getting the "input past end of file" error. my "second" code attempt, posted earlier:
...
BCMcsv = Dir$(BCMdrive & BCMtext)
Dim f1 As Long
Dim f2 As Long
Dim strLine As String
f1 = FreeFile
Open BCMdrive & BCMcsv For Input As #f1
f2 = FreeFile
Open BCMdrive & "tempBCM.txt" For Output As #f2
Line Input #f1, strLine 'ignore first line
Do While Not EOF(1)
Line Input #f1, strLine
Print #f2, strLine
Loop
Close #f1
Close #f2
...
does actually seem to work down to the "Line Input #f1, strLine" line, but it is reading the entire csv file at this point; doesnt seem to be parsing it line by line... ??
|
|
|
|
|
Not sure where you have taken into account that the delimiter is LF instead of CRLF in your code...
FYI,
Line Input
reads a line of text until it encounters a CR, which in this case is probably only at the end of the file.
Happiness will never come to those who fail to appreciate what they already have. -Anon
|
|
|
|
|
right, apparently not in this code. was trying to work with "sLines = Split(objTXT.ReadAll, vbCvLf)" in the first code above; cant seem to get either to work right
|
|
|
|
|
dubscat wrote: "sLines = Split(objTXT.ReadAll, vbCvLf)" Should it not be vbCRLF?
Also, did you try splitting with just vbLF? If your CSV is delimited with LF, surely that should work?
Happiness will never come to those who fail to appreciate what they already have. -Anon
|
|
|
|
|
yes, did, but was still getting the "input past end of file" error. here's the code again:
....
BCMcsv = Dir$(BCMdrive & BCMtext)
Set objFSO = CreateObject("scripting.filesystemobject")
Set objCSV = objFSO.CreateTextFile(BCMdrive & BCMcsv, True, False)
Set objTXT = objFSO.OpenTextFile(BCMdrive & BCMcsv, 1)
sLines = Split(objTXT.ReadAll, vbLf)
objTXT.Close
iNumberOfLines = UBound(sLines)
If iNumberOfLines > 1 Then
Set objTXT = objFSO.OpenTextFile(objCSV, 2) 'For Writing
For i = 1 To iNumberOfLines
objTXT.WriteLine sLines(i)
Next
objTXT.Close
End If
...Set objects = nothing ...
|
|
|
|
|
Which line errors out?
Happiness will never come to those who fail to appreciate what they already have. -Anon
|
|
|
|
|
got it, ChandraRam:
found the "fix csv..." solution on-line; link is below. final code:
...
BCMcsv = Dir$(BCMdrive & BCMtext)
'Fix CSV file to read correctly =====================================================================
Open BCMdrive & BCMcsv For Input As #1
Line Input #1, TxtLine 'read line input, which reads entire file because no CR Chr(13), just LF Chr(10)
NewText = Replace(TxtLine, vbLf, vbCrLf) 'Find all Chr(10) LF and replace with CRLR
Close #1
Open BCMdrive & BCMcsv For Output As #1 'Open file again, but for output this time
Print #1, NewText 'use print function to write contents, but with LF replaced by CRLF
Close #1
'======================================================================================================
Open BCMdrive & BCMcsv For Input As #1
Set objFSO = CreateObject("scripting.filesystemobject")
Set objTXT = objFSO.OpenTextFile(BCMdrive & BCMcsv, 1)
sLines = Split(objTXT.ReadAll, vbCrLf)
objTXT.Close
iNumberOfLines = UBound(sLines)
If iNumberOfLines > 1 Then
Set objTXT = objFSO.OpenTextFile(BCMdrive & BCMcsv, 2) 'For Writing
For i = 1 To iNumberOfLines
objTXT.WriteLine sLines(i)
Next
objTXT.Close
End If
set objects = nothing
...
http://us.generation-nt.com/answer/vba-line-input-not-recognizing-end-line-help-198799921.html[^]
|
|
|
|
|
If this "fixed" it, I don't see why it did not work when you said you tried the "Split" function with just the vbLF...
Anyway... glad your code works OK for you now.
Happiness will never come to those who fail to appreciate what they already have. -Anon
|
|
|
|
|
right, not sure either at this point, but is working fine now. for whatever reason, it rectified the 'input past EOF' error....
|
|
|
|
|
I'm writing an application that currently uses RSA public-key encryption. However, for users using newer computers, I want to offer the ability to use ECDH (Elliptic Curve Diffie-Hellman) as this seems to have many advantages over RSA. I've been over the MSDN articles and have tried search Google and CodeProject to figure out what I'm trying to do, but no luck.
Basically the application should do the following when starting up:
1. Check if the system supports ECDH. If it does continue on, otherwise create RSA keys.
2. Check if ECDH keys have already been created using a specified name.
2a. If the ECDH keys exist with the specified name then load them into an object to use within the application
2b. If the ECDH keys do not exist with the specified name then create ECDH keys (which I believe should be a CngKey object)
I've figure out how to make sure the ECDH keys can be created and even create them. However I can't figure out how to do the following:
1. Load the keys into an ECDH object. I check for the keys and load them like this:
If CngKey.Exist(keyName) then
Dim key as CngKey = CngKey.Open(keyName)
End If
However once I have a CngKey object how to I apply that to an ECDH object (the key property of an ECDH object is read-only)?
2. I can't seem to delete a key once it is created. I tried the following:
Dim b As Boolean = CngKey.Exists(keyName)
Dim key As CngKey = CngKey.Open(keyName)
key.Delete()
b = CngKey.Exists(keyName)
If I run the above code, the second time I check if the key exist using b = CngKey.Exists(keyName) it always return true. Reading the MSDN documentation, it seems that when you call the CngKey.Delete method, it just prevents the CngKey object from being used. How can I actually delete the key permanently? What do I need to replace key.Delete() with so when b = CngKey.Exists(keyName) is called the second time it would actually return false? Or is there no way to actually permanently delete a named key?
Any other documentation, articles, links, etc. that shows how to use ECDH in VB.NET would be greatly appreciated. In the key I'm just trying to find how to load and save ECDH keys like RSA keys, so my program has multiple options.
Thanks in advanced for any help you can provided.
|
|
|
|
|
Someone actually tell me what's the best method for deploying between clickonce and windows installing deployments methods
|
|
|
|
|
This has nothing to do with Visual Basic, please choose a more appropriate forum.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
That depends on your requirements. There is no such thing as a "best method" in all deployment cases.
|
|
|
|