Does anyone know COBOL and how to retrieve data from a txt file? I have a list of words (1 word per line) and i want to retrieve them one by one randomly using cobol. thanks.

Sorry, but the last time I did anything with COBOL was about 25 years ago... Try learning the language. The problem should not be difficult to solve, I would think. It is just basic I/O.

Member Avatar for Wazzza95

Hi,

Like many other languages, COBOL has many different implementations, each with varying syntax on certain commands. Whilst the core of it is/should be the same across implementations, there may, or rather will be, different ways of accessing data.

So this brings me to some questions:
- What system are you writing this for (computer, OS, compiler version, etc)?
- If the file is a text file, are you aware there is only a "brute force" way to retrieve a random record (ie read every record until you get the one you want)?
- Do you know the size of the file in (a) records, and (b) record width [longest word]?
- How do you want to randomly choose a record - is it the user selecting a number or are you using a random number generator, or some other means?
- How are you looping - (a) until the file is fully read, (b) predefined number, or (c) user input to confirm when done?

COBOL has no Random Number Generator, so it will have to be written. I tried years ago, but out of 20 numbers, I always got 1, 2 & 3 or 18, 19 & 20. You will be better off if you can read the date&time, to the second, and if possible down to 100ths of a second, to use as the seed for your generator, and that varies between COBOL compilers.

Cobol likes to read whole records from a file, not characters, so accessing a text file in Windows may be tricky, given the variable record length (I've never written for Windows, nor used variable records), but I expect it should still work.

If write some code, it will be based the system I use, and will probably need to be adapted to yours. I just want to make sure this is not part of some sort of assignment first.

Member Avatar for Wazzza95

Hi,

Further to my comments above, you can actually read records from within a file without reading all records beforehand. The file will need to be some sort of indexed file, and from my understanding, would be an ISAM file on an IBM system, or KSAM on an HP mid-range system, and so on.

Since there was no other response in two months, I assume this is no longer required. Is this the case?

COBOL random number generator:
When choosing a number of random numbers in a list, have a prime number of items, such as 19 or 23 in your list. This answer is based on bservation; I know of no good reason why this should be so.

VSAM is a good solution.
Another may be Relative Record Number (RRN). Super-fast, but not as versatile as VSAM.
With today's computer speeds, this may be irrelevant.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.