Managing data file for a lucky draw

Let's say I'm going to handle a lot of words which is stored in a plain text file.
Be sure that each line has only 1 word & there are more than hundred thousands of lines in total.

I have to write a program to random a word and store it in a STRING var.

Here is an example of what the content of the text file looks like:
[code]
Andy
Betty
Cindy
David
Eva
Frankie
Gigi
Harold
...
[/code]

Use FILE instead of TEXT because it is time consuming in reading STRING from the TEXT file.
[code]name_list : text ; (*I don't want text*)
name_list : file ; (*data file instead*)[/code]

Many thanks,
blackr1234

Comments

  • We can't wait to see how it comes out. Post what you get so we can check to see that it abides by your specifications.
  • I know text file works as well. But I would like to learn data file.

    For a text file, I can handle it with these commands to read the 5th line:
    [code]
    assign(text, 'file.txt');
    reset(text);
    FOR i:= 1 TO 5 DO
    readln(text, string1);
    writeln(string1);
    close(text);
    [/code]

    However, for data file, I may use these to read 1 character: (I'm not really sure)
    [code]
    assign(data, 'file.dat');
    seek(data, 0);
    read(data, byte1);
    writeln(chr(byte1));
    [/code]

    But now I want to read a WHOLE LINE, not only a byte and convert it into character.
    How can I do so?
  • As you write, you know how to do text files, but with the other types, it's a different way. With [color=Orange]untyped[/color], you use [color=Green]blockread[/color] and [color=Green]blockwrite[/color] to do file communications. Read all the ins and outs with this untyped. Then there is the [color=Orange]typed[/color] and it consist of a [color=Green]record type, integer, real, longint, or something like that[/color]. I've used this with a record and once you get use to how it's handled; it's almost better than the text or untyped form and you just use [color=Green]read[/color] and [color=Green]write[/color]s along with the assigned file.
  • Thanks for reply.
    I have seen an example from the [link=http://www.learn-programming.za.net/programming_pascal_learn11.html]Learn Pascal Programming[/link] website.

    [code]program DataFiles;

    type
    StudentRecord = Record
    Number: Integer;
    Name: String;
    end;

    var
    Student: StudentRecord;
    f: file of StudentRecord;

    begin
    Assign(f,'MyFile.dat');
    Rewrite(f);
    Student.Number := 12345;
    Student.Name := 'John Smith';
    Write(f,Student);
    Close(f);
    end.[/code]

    The example above is for writing in data. So can I do the reverse to read data line by line from a file?

    I have tried but I can't run this line: read(f, line);

    [code]program read_dat;

    USES crt;

    TYPE
    xr = RECORD
    name: STRING;
    END;

    var line: xr;
    f: file of xr;

    BEGIN
    assign(f,'C:f.dat');
    reset(f);
    seek(f, 0);
    read(f, line); {cannot run, it gives me error code 100}
    close(f);
    writeln(line.name);
    readkey
    END.[/code]

    Content in C:f.dat:
    [code]abc
    def[/code]
  • Okay, nice try. How did you create C:f.dat? Use a regular editor to do it? Look at your record definition; [color=Green]name[/color] is defined as a [b]STRING[/b]. How long is a STRING? Well, it's 255 bytes with a leading byte that is the character count. Sooooo ... first write out some records the way that you want to. Then bring up DEBUG and read in the file and look at it carefully. If you don't fill in the string fully then the program puts in it's own stuff or junk. And look at the leading byte, the hex value is going to be whatever you put in the string. But if it doesn't fill in the string completely then the program fills it in.
  • I use blockread/blockwrite for typed and untyped files as well. Reason is that you can transfer data in bulk and they are way faster than what read/write could do. If you have to store hundred thousands of lines and you want to be able to access a random line, you cannot use a text file unless you have the time to skim through each line till you get to the one you seek. With a set record length its easy to calculate the corresponding position in a file. Here's a small piece of code how to use it:[code][color=Blue]type str20=string[20]; { record length = 21 bytes --> 1 length marker + 20 data }


    var f:file; { untyped file }
    s:str20; { string variable of a set length }
    i,j:word;

    begin

    { Adding names to file }
    assign(f,'name.dat');
    rewrite(f,1); { set record size to 1 }
    i:=1;
    writeln('Enter names, empty line to end');
    repeat
    write('Name no ',i,' >');readln(s);
    if s<>'' then begin
    blockwrite(f,s,sizeof(str20)); { <---- }
    inc(i);
    end;
    until s='';
    dec(i);
    close(f);writeln(#13#10);
    if i>0 then begin

    {Retrieving a random name }
    randomize;
    j:=succ(random(i));
    reset(f,1); { After reset file position is 0 }
    seek(f,(j-1)*sizeof(str20));
    blockread(f,s,sizeof(str20)); { <---- }
    writeln('Record no:',j,' is: ',s);
    close(f);


    end else writeln('File is empty');
    readln;
    end.[/color][/code]
  • Thanks for you both's answers.

    But I think we have gone too far.
    I would say that my aims are:

    1. use of FILE variable instead of TEXT
    2. READ the data file "line by line" (a finite loop of readln in the case of text file)
    3. The content of a line is stored in a STRING variable.

    Just tell me how to do these. Could you, please, simplify your answers?
    I'm sorry to say that I get more question marks when reading those codes because my teacher hasn't taught me the usage of data file.

    Tell me if it's not possible, too.
  • :
    : 1. use of FILE variable instead of TEXT

    Use: var f:file of str20;

    : 2. READ the data file "line by line" (a finite loop of readln in the
    : case of text file)[code]var i:longint;
    s:str20;

    for i:=1 to number_of_record_you_seek do read(f,s);
    writeln(s);[/code]Note, that this will take forever if you got hundreds of thousands of records.

    : 3. The content of a line is stored in a STRING variable.

    str20 is a [b]STRING[/b] variable with a length limit of 20 characters



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories