|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Newbisimo: VB6 File I/O Question
Although I have several years of experience with C/C++, I am just starting on VB6 through self-tuition. As part of that effort, I want to convert a C utility over to a VB6 application.
I have a text file containing several articles. Each article's title line starts with a unique character (@). My C utility scans the file for each title line, uses the ftell function to get its file pointer (its location within the file), and saves the title and its file pointer in a list. For now, the program picks a title at random, uses the fseek function to reposition to that article, and outputs the article to an output file. With VB, I would want to be able to review the list and pick out articles to output -- but that would be the easy part. What are VB6 equivalents to ftell and fseek? How does one obtain one's position within a file and then reposition back to an arbitrary position? Yes, I did try Google'ing for it, but with no success. Also, since each paragraph is one line, I use an arbitrarily large string buffer in my C utility. Are strings in VB6 still limited to 255? |
|
#2
|
||||
|
||||
|
So far as I know, yes. Interesting that you should make the transition from a C Programmer to VB. Most C Programmers I know are so anti-MS that they wouldn't even start an app if they knew it was written in VB... are you sure that you're not crossing the line to the dark side?
as for your question... I have no idea. You could try reading the line a character at a time in a loop - starting at the @ symbol and breaking at the next one, until EOF(FileNumber). Once you reach one of those conditions, you would have your constructed string and could put it in an array?
__________________
Fisherman "Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." - A.Einstein |
|
#3
|
||||
|
||||
|
Part of the situation is that I will be taking over somebody else's project, part of which was done in VB6. Besides, a VB app need not be entirely in VB; the real code can still be done in C and reside in a DLL. Not all that is gold glitters and not all who wander are lost (I don't know why that just came back to me).
But the basic question is whether VB6 has a mechanism for repositioning where in the input file you read from next. For example: Article 1 is at 0 bytes Article 2 is at 8779 bytes Article 3 is at 17655 bytes Article 4 is at 25565 bytes Article 5 is at 40920 bytes Article 6 is at 46897 bytes Article 7 is at 54444 bytes Article 8 is at 60997 bytes Article 9 is at 68180 bytes Article 10 is at 76314 bytes Article 11 is at 82811 bytes Article 12 is at 89321 bytes Article 13 is at 93321 bytes Article 14 is at 101121 bytes ... Article 254 is at 2498603 bytes First, how in VB6 can I even tell that Article 10 is positioned at 76,314 bytes? Second, how in VB6 would I go directly to 68180, then to 101121, then to 8779, etc? In C, you would use ftell() and fseek() respectively. What would you use in VB6? |
|
#4
|
||||
|
||||
|
I was about to give up on VB having basic file I/O support and to decide to get the real work done in a C DLL. That would have been a bummer, since part of the purpose of this exercise is to learn VB coding.
And I would have given up if I hadn't found an entry for "file pointer" in the index of O'Reilly's "VB & VBA in a Nutshell". VB's version of ftell is the SEEK function and of fseek is the SEEK statement. With that, I was finally able to find the right pages in the help files: Quote:
I also verified that the String data type is no longer limited to 255 characters, but rather can approach 64K. |
|
#5
|
|||
|
|||
|
U will random access a file with vb,So u can use Get function:
Get [#]filenumber, [recnumber], varname recnumber can be postion of the file... U can find help in the vb help or MSDN... |
|
#6
|
||||
|
||||
|
You shouldn't be using the old fiel I/O mech. anymore unless you are accessing binary files. You should be using the FileSystemObject and it's subsidory objects. This is on the 'Microsoft Scripting Run-time' or Scrrun.dll.
There is a TetStream object which acts as the I/O for text files, this has a ReadLine method and a Line property. Have a look at the URL below. http://support.microsoft.com/defaul...118&Product=vb6 Hope this helps Dave |
|
#7
|
||||
|
||||
|
Quote:
My understanding is that that only works with fixed-length records. I'd be dealing with variable-length records in which each "record" is either an article or a paragraph within that article (and, of course, there'd be a variable number of paragraphs per article). |
|
#8
|
||||
|
||||
|
Quote:
OK, I read that page which didn't tell me much except that I have to drag another run-time into the picture. And I Google'd and got lots of hits in lots of different languages saying how easy FileSystemObject. And I read the VB6 help file on FileSystemObject and TextStream. And I Google'd on "VB6 TextStream seek" and didn't get anything relevant. So how am I supposed to do the equivalent of the SEEK function and statement when using TextStream? The closest thing to the SEEK function that I could find was the Line property, but I could not find any method for positioning to a specific line. I kind of have this utilitarian attitude that, no matter how fashionable a feature is or how really easy it is to use, if it does not perform the required function then it is worthless. And what I am looking for is a fairly basic file I/O functionality. Thank you for the information, but so far, I cannot see FSO or TextStream having any worth. |
|
#9
|
||||
|
||||
|
Although the FSO does come in handy when dealing with typical file operations, for what you are wanting to do I believe you have found the best route with SEEK.
The FSO is good when it comes to dealing with directory dealing with with standard I/O file operations, but it is sometimes lacking when it comes to the more advanced sections like seek. |
|
#10
|
||||
|
||||
|
I don't know how fasionable fso is, I haven't managed to pull any birds with it or get into any trendy clubs (though I haven't tried).
I haven't much experience of C and so may not have understood the SEEK method. It will take you directly to a line number in a text file. TextStream has the SkipLine method. To skip back though the file needs to be explicitly closed and reopened. I don't really have much experience of the old I/O though, so you are right, there may be a more intuative method using that. I would emulate the SEEK method like this though: Function Seek(Index As Integer,Ts as TextStream) as String On Error Goto ErrorHandler 'In case a backwards index is given Static OldIndex As Integer 'Last index called Dim i As Integer 'Counter Dim NumOfLinesToSkip As Integer NumOfLineToSkip = Index - OldIndex For i = 0 To NumOfLinesToSkip Ts.SkipLine Next i Exit Function ErrorHandler: Err.Raise End Function I agree with onslaught in that it can be seriously frustrating at times One, last thing, the strings in VB can be almost any size. |
|
#11
|
|||
|
|||
|
Hello,dwise1_aol !I think u maybe have the article length like you said:
Article 1 is at 0 bytes Article 2 is at 8779 bytes Article 3 is at 17655 bytes Article 4 is at 25565 bytes Article 5 is at 40920 bytes Article 6 is at 46897 bytes Article 7 is at 54444 bytes Article 8 is at 60997 bytes Article 9 is at 68180 bytes Article 10 is at 76314 bytes Article 11 is at 82811 bytes Article 12 is at 89321 bytes Article 13 is at 93321 bytes Article 14 is at 101121 bytes ... U can read one article by get function like it. read article 1: dim articlestr1 as string*8779 GET #1,0,articlestr1 read article 2: dim articlestr2 as string*(17655-8779) GET #1,8779,articlestr2.... |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > Newbisimo: VB6 File I/O Question |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|