473,326 Members | 2,108 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,326 software developers and data experts.

How to speed up reading input from a text file?

MLH
I use the following code blurb to read text from a file
on disk, saving it to a string var...

20 Open MyFile For Input As #1 ' Open file.
30 Do While Not EOF(1) ' Loop until end of file.
40 MyChar = Input(1, #1) ' Get one character.
50 strFileContent = strFileContent & MyChar
60 Loop
70 Close #1 ' Close file.

It is quite slow. Actually, I can read the same info
from a web server under the restrictions of DSL bandwidth 10x
faster than I can read it straight from my local drive. Are there
ways to modify my code that will speed up the process?
Nov 13 '05 #1
2 2737
Am Tue, 06 Sep 2005 12:00:11 -0400 schrieb MLH:
I use the following code blurb to read text from a file
on disk, saving it to a string var...

20 Open MyFile For Input As #1 ' Open file.
30 Do While Not EOF(1) ' Loop until end of file.
40 MyChar = Input(1, #1) ' Get one character.
50 strFileContent = strFileContent & MyChar
60 Loop
70 Close #1 ' Close file.

It is quite slow. Actually, I can read the same info
from a web server under the restrictions of DSL bandwidth 10x
faster than I can read it straight from my local drive. Are there
ways to modify my code that will speed up the process?


Hello,

maybe the following does the trick, but only if there are not more then
64K characters in the file.

40 strFileContent=input(lof(1),#1)
lof() is length of file. Maybe the functions name is not correct.

HTH
Karpi
<fluctuat nec mergitur>
rem line 50
Nov 13 '05 #2

"MLH" <CR**@NorthState.net> wrote in message
news:8s********************************@4ax.com...
I use the following code blurb to read text from a file
on disk, saving it to a string var...

20 Open MyFile For Input As #1 ' Open file.
30 Do While Not EOF(1) ' Loop until end of file.
40 MyChar = Input(1, #1) ' Get one character.
50 strFileContent = strFileContent & MyChar
60 Loop
70 Close #1 ' Close file.

It is quite slow. Actually, I can read the same info
from a web server under the restrictions of DSL bandwidth 10x
faster than I can read it straight from my local drive. Are there
ways to modify my code that will speed up the process?

You can't be that surprised, can you? You are going to the hard disk,
reading a single character, adding it to a string and repeating this
read-from-disk operation thousands of times. Instead of grabbing one
character at a time, grab the lot in one go:

Public Function GetContents(strPath As String) As String

On Error GoTo Err_Handler

Dim intFile As Integer
Dim strContents As String

intFile = FreeFile

Open strPath For Binary As #intFile

strContents = String(LOF(intFile), vbNullChar)

Get intFile, , strContents

GetContents = strContents

Exit_Handler:

On Error Resume Next

Close intFile

Exit Function

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
30
by: Przemo Drochomirecki | last post by:
Hi, The task was indeed simple. Read 2.000.000 words (average length = 9), sort them and write it to new file. I've made this in STL, and it was almost 17 times slower than my previous...
18
by: Michael | last post by:
Hi, I moved to c++ from c, and wanted to know what the best way to read data from files is in c++. Any thoughts? fscanf() is possible but fairly painful! Regards Michael
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...
10
by: T Cordon | last post by:
I am using a StreamReader to read text from an HTML file and display it as part of a page in a Label Control. Buy it is not displaying characters as: ñ, ó, ú, etc. Please Help. Thanks
9
by: jeff M via .NET 247 | last post by:
I'm still having problems reading EBCDIC files. Currently itlooks like the lower range (0 to 127) is working. I have triedthe following code pages 20284, 20924, 1140, 37, 500 and 20127.By working I...
1
by: j7.henry | last post by:
I am trying to pull specific data that is in a comma delimited file into a web page. So if my comma delimited file looks like: Name,Address,Zip Fred,123 Elm,66666 Mike,23 Jump,11111 I would...
8
by: SaltyBoat | last post by:
Needing to import and parse data from a large PDF file into an Access 2002 table: I start by converted the PDF file to a html file. Then I read this html text file, line by line, into a table...
21
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.