我试图从masm程序集x86中包含500个单词的文本文件中读取,任务是从那里随机选择8个单词。有人可以告诉我怎么做吗?
这是从文件中读取并在控制台上显示的代码。
;READING FROM THE FILE
invoke CreateFileA,offset filepath,1,0,0,3,128,0
invoke ReadFile,eax, offset buffer,lengthof buffer, offset x,0
;DISPLAYING CONTENT OF BUFFER ON CONSOLE
invoke GetstdHandle,-11
invoke WriteConsoleA,eax, offset buffer, lengthof buffer, offset x,0
以下是该文件中的一些词语:
1. abacus
2. abacuses
3. abaft
4. abalone
5. abalones
6. abandon
7. abandoned
8. abandonedly
9. abandonee
10. abandoner
11. abandoners
12. abandoning
13. abandonment
14. abandonments
15. abandons
16. abase
17. abased
18. abasedly
19. abasement
20. abaser
21. abasers
22. abases
23. abash
24. abashed
25. abashedly
26. abashes
27. abashing
28. abashment
29. abashments
30. abasing
31. abatable
32. abate
33. abated
34. abatement
35. abatements
36. abater
37. abaters
38. abates
39. abating
40. abatis
41. abatises
42. abator
43. abattoir
44. abattoirs
45. abbacies
46. abbacy
47. abbatial
48. abbe
49. abbes
50. abbess
答案 0 :(得分:3)
没有直接的方法从文本文件中获取任意行。每行可以有不同的长度,Windows不会跟踪行开始的位置。
解决这类问题的一般方法是读取整个文件,沿途随机挑选线条。通常,reservoir sampling方法用于此目的。它很容易实现,并小心避免任何可能的采样偏差。
以下是您可能想要实现的相关algorithm R的伪代码:
Algorithm to pick k lines at random from file f:
Let A be an array holding j lines.
Read the first k lines from f and store them in A.
Until f is empty:
Read a line from f and let i be the number of lines you read so far.
Pick a random number j between 1 and i inclusive.
If j <= k then assign the line you just read to A[j].
Return the lines in A.