Inc LineNum ;. ASerge Hero Member Posts: Quote from: Bart on January 22, , pm. Quote from: ASerge on January 23, , am.
Pet hate: people who boast about the size and sophistication of their computer. I think most basic codes would look like as following. I'm just using the inherent functionality of read statement which does convert itself. There are issues of whether more than one figure are allowed in one line. Also displaying in memo, and using ShowMessage as well.
Sign Up — It's Free! Reply to this Topic. This topic is old! No one has contributed to this discussion in over 5 years.
Are you sure you have something valuable to add to revive the existing conversation? Consider starting a new topic instead. The contents of the loop seem to be trying to do two things: saving the current line in the array, and printing it to the console. I assume the latter is only for debugging? It is not thread-safe, possibly slow, handles Unicode badly, etc. It was used in the 90s, but shouldn't be used today. Instead, use the facilities provided by your RTL.
ReadAllLines , streams, etc. If you want to fix the inefficient array issue, but still not use modern classes, allocate in chunks:. But if you have access to modern classes, things get much easier. The following examples use the modern Delphi RTL:.
As you can see, the modern approaches are much more convenient less code. They are also faster and give you Unicode support. Note: All snippets above assume that the file contains at least a single line.
Yes it does. But you don't write it to the console. See the third line, readln x, s ;. I am trying to read a file and randomly print it to console. To get a random value from the array, use Randomize to initialize the random generator, and Random to get a random index.
Nonetheless, for your reading an extra line issue, you happened to read a line before going into your read loop. So this is your program from the begin to the end statement without that extra readln.
For this code the routine is simple and there is actually more than one method that I can think of. For the first method, you can read each line into an array. Then traverse through the array and create a random number of 0 or 1. If it is 1 print that line. For the second method, each time read a line from the file, generate a random number of 0 or 1.
If that random number is a 1, print that line. Take note to use randomize before you run random to not get the same random number of the last program execution. The following example creates and writes into a text file named contact. Appending to a file means writing to an existing file that already has some data without overwriting the file. Pascal - File Handling Advertisements. Previous Page. Next Page.
0コメント