473,387 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

reading different sections of a text file at a time

How can I read only a section of a text file at a time.Say I have a text file of 1000 rows as database and I want to read from row 1 to 200 only, then maybe from 201- 275 and so on.
What is the code that would use if I create a button that says 1-200.

Thanks
Jul 4 '07 #1
17 1866
Dököll
2,364 Expert 2GB
How can I read only a section of a text file at a time.Say I have a text file of 1000 rows as database and I want to read from row 1 to 200 only, then maybe from 201- 275 and so on.
What is the code that would use if I create a button that says 1-200.

Thanks
This is a nicely detailed post, mustakbal!

I will try to see if can find you an example, something may already been posted I'll search there too.

Please stay tuned, welcome!
Jul 4 '07 #2
Dököll
2,364 Expert 2GB
Just tried and it works at my end:

(1) Add a command button to your form, add a loop
(2) I have three labels, you can modify, Label1(0), and so on...
(3) You can dimension found, Linenum, and pos, Dim as integer seems to work okay

The result is you get the number of line in the document red through a multiline box (Text1.Text). Sorry this is reading it reverse, could not find a good example
Expand|Select|Wrap|Line Numbers
  1.  
  2. Do Until found = 0
  3.    found = InStrRev(Text1.Text, Chr(10), pos, 0)
  4.    If found = 0 Then Exit Do
  5.    Linenum = Linenum + 1
  6.    pos = found - 1
  7. Loop
  8. Label1(0).Caption = Linenum
  9.  
You should also assign values:

Expand|Select|Wrap|Line Numbers
  1.  
  2. found = -1    
  3. Linenum = 1  
  4. pos = Text1.SelStart    
  5.  
  6.  
Add a lengthy letter in the Text1.Text mutiline textbox and fire command button, Label1(0) should tell you how many lines are in the document.

There are other ways to do this, please let us know.
Jul 4 '07 #3
Thanks Dököll for your offer to help. I have been trying to make the code above work with no success yet. But I still don't see where I am setting the limit of high and low number of rows. By the way I am a newbie also in vb6 ! so you might need to do the step-by-step instructions with me!

When I open a file that has 1000 lines in it and I declare the number of lines as 1000, and then wanted to randomly call different lines, it will be picking them from among the numbers 1 through 1000. Now, how do I set the upper and lower limits (say 200 as the low and 600 as high) and have the program call randomly only from the lines 200 throgh 600.

If I can do that with this code where would the 200 and 600 go in the code.

Thanks in advance.
Jul 4 '07 #4
Killer42
8,435 Expert 8TB
You could use any code you like to read the file (there's a simple code snippet to do this in the Articles section - you can find it in the Index up the top of this forum).

Assuming you are reading from the start of the file, all you have to do is count the lines as you read them. If you want to process lines 201-700, just skip each line until your count reaches 201. Then when your count hits 701, drop out of the loop.

It would probably be both faster and more efficient (unless the file is too large) to just read all of the lines into an array. Then you can pick and choose from the array, however you like.

On the other hand, if the lines are all the same length you could open the file in "random access" mode which allows you to just straight to any line, by number. In other words, you could tell VB to open the file, jump to the 384th line, read it, and close the file, without accessing any other lines in the file. But only if the lines are of identical length. (Note that if you did want to go this way you could always pad the lines with spaces or something to make them the same length.
Jul 5 '07 #5
O K,Killer42..we are on the right track but please keep in mind that you are dealing with a newbie. The part about : "If you want to process lines 201-700, just skip each line until your count reaches 201. Then when your count hits 701, drop out of the loop." is what I am interested in doing..but how do I set these limits in the progrogram and then have that followed by the program choosing entries at random from this set only (201-700).Does the program have to go through the skipping procedure each time it chooses another random entry or is there a better way of doing that.

That's where I am stuck right now trying to write the code
1- set the limits and
2- have entries randomly chosen from between these numbers.

By the way I have the code for choosing random entries from the whole file but not parts.

Thanks
Jul 5 '07 #6
Expand|Select|Wrap|Line Numbers
  1. 'Try This
  2.  
  3. 'On your Form
  4. ' Place a command button called CmdRead
  5. ' Place three text boxes called txtMyFile, txtstartline, txtendline, txtReadLine
  6. ' Type your file name (including path and the extension of the file) in txtMyFile
  7. ' or alternatively type the file name (including path and the extension of the file) in quotation marks inplace of txtMyFile.text like "C:\Data\myFileNametoRead.txt"
  8. ' Type first and last line numbers you want to read in txtstartline and txtendline respectively
  9. ' Click CmdRead
  10.  
  11. 'Unfortunately VB cannot handle fixed field length sequential files like QuickBasic used to
  12. ' because of that we can not jump to a location in a file
  13.  
  14.  
  15. Private Sub CmdRead_Click()
  16. Dim startline, endline, currentLine As Integer
  17. Dim LineRead As String
  18. startline = txtstartline: endline = txtendline
  19. currentLine = 0:
  20. If startline < 1 Then startline = 1
  21. If endline < 1 Then endline = 1
  22.  
  23. Close #1: Open txtMyFile.Text For Input As #1
  24.  
  25. Do While currentLine < startline
  26.     If EOF(1) <> -1 Then Input #1, LineRead Else GoTo endFile: 'Check if end of file
  27.     currentLine = currentLine + 1
  28. Loop
  29.  
  30. Do Until currentLine > endline
  31.     If EOF(1) <> -1 Then Input #1, LineRead Else GoTo endFile: 'Check if end of file
  32.     txtReadLine.Text = CStr(currentLine) & ": " & LineRead
  33.     MsgBox CStr(currentLine) & " OK", vbOKOnly
  34.     currentLine = currentLine + 1
  35. Loop
  36.  
  37. Finish:
  38. MsgBox "All Done", vbOKOnly
  39. Exit Sub
  40.  
  41. endFile:
  42. MsgBox "end of File", vbOKOnly
  43.  
  44. End Sub
Jul 5 '07 #7
1fromOZ]
This worked great... but can I code a button so that clicking it would set the lower and upper limits as 1-100 and another button as 101-250 and so on. and have this code in the program so that if I wanted to work with 101-250 set I would click the second button.
I think the answer is in your code but I haven't figured it ouit yet.
Jul 5 '07 #8
Killer42
8,435 Expert 8TB
Unfortunately VB cannot handle fixed field length sequential files like QuickBasic used to because of that we can not jump to a location in a file
I'm afraid that is simply not true.

VB probably does handle them slightly differently - it's been quite a few years, I can't remember QB in that much detail. But you can open as random (random-access fixed-length record file) and jump directly to any record you want. If you open as Binary, you can jump directly to any byte in the file and start reading.
Jul 5 '07 #9
Killer42
8,435 Expert 8TB
By the way I have the code for choosing random entries from the whole file but not parts.
Surely all you have to do, then, is check that the number of the line you are going to retrieve is within the desired limits. You shouldn't need to make any change in the file handling.

Let's assume you have two variables called LowerLimit and UpperLimit, defining the section of the file you're allowed to read. You then decide to go and get line number RequestedLineNumber...

Expand|Select|Wrap|Line Numbers
  1. Select Case RequestedLineNumber
  2.   Case LowerLimit To UpperLimit
  3.     ' Do whatever it is you do now.
  4.   Case Else
  5.     ' Return an error, or whatever.
  6. End Select
You could also have used an IF...ELSE of course. I just happen to like Select Case because of the way it allows you to define a range using the To clause.
Jul 5 '07 #10
It seems like this should work..unfortunately I can't check it right now because I have to leave for a couple of hours.
I'll check it later and post my progress..Please check back later to see if I am stuck again!!
Appreciate all your help and suggestions.
Jul 5 '07 #11
Killer42
8,435 Expert 8TB
Glad to help. :)

Even if you don't need any more help, pop back and let us know how it went.
Jul 5 '07 #12
I am back ..it seems like after I set the upper and lower limits, the random statement for choosing the next item doesn't work anymore:

Int ( Rnd * uppernumber ) +1

as this was useful for numbers between 1 and 1000. What needs to be changed here to control that the numers that are chosen are between 600 and 900 for example?
Jul 5 '07 #13
I got it ! finaly.. I had to change the statement to read
lowerlimit+ Rnd * (upperlimit - lowerlimit)

and it seems to work..

Thanks again for your help and I '' be dropping by to learn from this forum...Seems people quite helpful here.
Jul 5 '07 #14
Killer42
8,435 Expert 8TB
I got it ! finaly.. I had to change the statement to read
lowerlimit+ Rnd * (upperlimit - lowerlimit)
and it seems to work..
Well done! :)

There's a code sample you could have copied from our VB Articles section which provides a function to return a random number within specified limits. But it just does the same thing you described here, and I believe it's much more satisfying when you work it out for yourself. (One thing which may be of interest - I've recently added another version which doesn't repeat numbers.)

Thanks again for your help and I '' be dropping by to learn from this forum...Seems people quite helpful here.
That's why the forum's here. :)

Most people (including myself) first come here to ask a question, and eventually move on to answering them. Just pop in when you can spare the time, and before you know it you'll be spotting questions that make you think "hang on, I know that".
Jul 5 '07 #15
gdijim
3
Thank you very much for these comments and code - I have been trying to do something similar in VB .net for some time with no luck.

Thanks!

Jim
Jul 6 '07 #16
Great ending for the thread.It couldn't have been "scripted" any better.Another person benefits from the same thread.
gdijim], great that you found it helpful.I've been working on this for a while too and visited many forums.This place though tops them all because the help was right there, right away thanks to moderators like Killer42. By the way,I found the article you mentioned and will probably make use of it.

Thanks again
Jul 6 '07 #17
Killer42
8,435 Expert 8TB
Thanks, it's good to see such positive feedback. I think we're all here because we're developers who have needed help with programming problems at some time, and stuck around to give something back.

Of course, not everyone gets an immediate (or indeed any) answer to their questions. Now and then we just don't have the answer, or the person who does know may be offline for a while. That's why it's important that as many people as possible get involved, and share their experience.
Jul 8 '07 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: jamait | last post by:
Hi all, I m trying to read in a text file into a datatable... Not sure on how to split up the information though, regex or substrings...? sample: Col1 Col2 ...
1
by: s99999999s2003 | last post by:
hi i used ConfigParser to read a config file. I need the config file to have identical sections. ie : blah = "some server" blah = "some destination" end= ''
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
2
by: kaosyeti | last post by:
hey... i have a quarterly report that i'm working on where each of the 12 or so pages is completely different from the other. all are based on basically the same info (ergo, the same query) but...
7
by: cmfvulcanius | last post by:
I am using a script with a single file containing all data in multiple sections. Each section begins with "#VS:CMD:command:START" and ends with "#VS:CMD:command:STOP". There is a blank line in...
7
by: random guy | last post by:
Hi, I'm writing a program which creates an index of text files. For each file it processes, the program records the start and end positions (as returned by tellg()) of sections of interest,...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
2
by: Derik | last post by:
I've got a XML file I read using a file_get_contents and turn into a simpleXML node every time index.php loads. I suspect this is causing a noticeable lag in my page-execution time. (Or the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.