Announcement

Collapse
No announcement yet.

Restart at beginning

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Restart at beginning

    I have a data file containing thousands of first names. I
    randomly generate a number between 1 and 17547. Let's say
    the number is 500. Then I will read the file and count the names
    until the 500th name comes up.

    If the user does not like it and wants a different name, is
    there a command that will tell the program to start reading
    the file from the beginning again? Like RESTORE for data lines?

    I can work with CLOSE and just OPEN the file again, but is there
    another way to start reading from the beginning again I wonder?

    Thank you.

    Robert Carneal

    ------------------

  • #2
    Well, I can think of a couple of things. Both require opening
    the file in binary mode

    If the records in the data base are of fixed length, say 15
    characters per record, generate the random number and multiply
    that by 15 then subtract 15. i.e. n = int((n * 17547) * 15) - 15
    and SEEK #1,N. One times 15 = 15 which would preclude selecting
    the 1st entry in the data base. You would have to take Cr/Lf
    into consideration if they are there.

    Or you just could use the SEEK command to go to the 1st record
    in the file. i.e. SEEK #1,0

    I'm sure there are other methods of achieving your goal but this
    gives you a couple of options to consider.

    ------------------




    [This message has been edited by Mel Bishop (edited August 10, 2002).]
    There are no atheists in a fox hole or the morning of a math test.
    If my flag offends you, I'll help you pack.

    Comment


    • #3
      Load them into an array. X =(Random Number) then name = namearray(x).
      Of



      ------------------
      Thanks,

      Bradley Callis
      Thanks,

      Bradley Callis

      Comment


      • #4
        You don't need BINARY access... a RANDOM access file would be vastly easier to use and understand...

        Simply store the first name list in a fixed record-length file...

        Code:
        TYPE RecordType
          FirstName  AS STRING * 20
        END TYPE
        
        DIM Record AS RecordType
        OPEN "Firstnam.dat" FOR RANDOM AS #1 LEN = SIZEOF(Record)
        FOR n=1 to NumNames
          LSET Record.firstname = (whatever) 
          PUT #1,N, Record
        NEXT
        CLOSE #1
        .. and to retrieve a random record, get it by record number:

        Code:
        RandomNo = (however you get the random number)
        GET #1, randomNo, Record
         INPUT " How about this one:" & Record.firstname & "?"; X$
         IF X$ = "Y" THEN
           ' yup, do this one
         ELSE
            whatever
         ....
        MCM


        Michael Mattias
        Tal Systems Inc. (retired)
        Racine WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          The SEEK statement works with files opened in sequential mode. Just use

          SEEK #x,0

          where x is the file handle, and the second parameter is the offset from the beginning of the file. By using 0, you'll go back to the beginning of the file.

          That said, I agree with Michael and Mel that random access or binary mode file access would be better choices.

          Alan

          ------------------
          Alan C. Earnshaw
          Information Management Systems, Inc.
          http://www.infoms.com
          Alan C. Earnshaw
          Information Management Systems, Inc.
          http://www.infoms.com

          Comment


          • #6
            Long time no see, Alan.

            That said, the problem with SEEK is, "SEEK where?"

            The only reason to use sequential access is that you have variable-length records terminated by CRLF. With variable-length records, record one obviously starts at offset (SEEK point) zero; but subsequent records may start anywhere and there is no way to 'calculate' the correct SEEK point. About all you could do to make SEEK useful with sequential access is read the whole file and save the SEEK point for each record in an array of INTEGERs or LONGs; then you could SEEK #hFile, SeekPoint%(n) to position the file pointer for a LINE INPUT which would get you record 'n'.

            Of course, that might be a perfectly acceptable solution for this application if the firstname file does not change during program execution.

            MCM


            Michael Mattias
            Tal Systems Inc. (retired)
            Racine WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              Ahh, but the original question was "...is there another way to start reading from the beginning...". There is, and that is SEEK #handle,0. As you noted, there are better ways to organize and read data files. But SEEK does what Robert wants to do.

              Alan

              ------------------
              Alan C. Earnshaw
              Information Management Systems, Inc.
              http://www.infoms.com
              Alan C. Earnshaw
              Information Management Systems, Inc.
              http://www.infoms.com

              Comment


              • #8
                ..the original question was "...is there another way to start reading from the beginning...".
                So you answered the <U>actual</U> user question? I guess that's why you are in software support and I'm not.

                That, or I just knew instinctively that "going back to the begining so I can read everything sequentially again" was a non-optimal solution and subconsiously chose to ignore it.

                MCM
                Michael Mattias
                Tal Systems Inc. (retired)
                Racine WI USA
                [email protected]
                http://www.talsystems.com

                Comment

                Working...
                X