473,659 Members | 2,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading different sections of a text file at a time

25 New Member
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 1879
Dököll
2,364 Recognized Expert Top Contributor
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 Recognized Expert Top Contributor
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
mustakbal
25 New Member
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 Recognized Expert Expert
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
mustakbal
25 New Member
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
1fromOZ
2 New Member
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
mustakbal
25 New Member
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 Recognized Expert Expert
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 Recognized Expert Expert
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 RequestedLineNu mber...

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

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

Similar topics

7
2778
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 Col3 Col4 A0012430 REKAL TVĂ„TTMEDEL EKOMAX 0,5L ST 75.9000
1
2050
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
6748
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 listen up; Reports, the way I look at them, all present data downwards, in this way; TITLE data
7
6055
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 populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
2
1465
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 the layout of the controls are wildly different from each other. right now, i've created each page of this report as its own report, just to get me started somewhere. what's the best way to open these 12 reports so that they all show up at the...
7
1621
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 between each section. I'm looking for the best way to grab one section at a time. Will I have to read the entire file to a string and parse it further or is it possible to grab the section directly when doing a read? I'm guessing regex is the best...
7
3059
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, and then some time later uses these positions to read the interesting sections from the file.
13
3694
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 consists of structures of type---- struct record { int acountnum; char name; float value;
2
2834
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 wireless where I'm working could just be ungodly slow-- which it is.) Is reading a file much more resource/processor intensive than, say, including a .php file? What about the act of creating a simpleXML object? What about the act of checking the...
0
8330
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8850
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.