Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Data files

  1. #1

    Data files

    It's been bugging me for some time, but it's a pretty basic problem really.

    e.g.
    type students = record
    ....
    ....
    file1 = file of student

    var student: students;
    dat: file1;
    ....


    As you can see here, I have a data file that is a file of a record.
    My problem is - I have a procedure for adding students and it works fine. But the program needs to have a procedure (it doesn't HAVE to be a procedure) in which you change their data (e.g. address, phone number etc.) and I'm just stuck.

    Can you even change stuff once it's all written in the file? How?
    If not, how should I get around the problem?

    Thanks!

  2. #2

    Re: Data files

    It's simple. First of all, you're using fixed-size records, so that you can easily seek in your file. I'd suggest you using streams. So to write anything, you use:

    [pascal]
    type
    TRecord = record
    MyInteger: Integer;
    MySingle: Single;
    MyString: ShortString;
    end;

    procedure WriteFile;
    var
    FS: TFileStream;
    Rec: TRecord;
    begin
    FS := TFileStream.Create('C:\myfile.dat', fmCreate);
    try
    Rec.MyInteger := 10;
    Rec.MySingle := 3.14;
    Rec.MyString := 'Simple string';

    FS.Write(Rec, SizeOf(TRecord));
    finally
    FS.Free();
    end;
    end;
    [/pascal]

    Now, to modify a record:
    [pascal]
    procedure ModifyRecord(const ARecordIdx: Cardinal; const ARecord: TRecord);
    var
    FS; TFileStream;
    begin
    FS := TFileStream.Create('C:\myfile.dat', fmOpenReadWrite + fmShareExclusive);
    try
    FS.Seek(ARecordIdx * SizeOf(TRecord), soFromBeginning);
    FS.Write(ARecord, SizeOf(TRecord));
    finally
    FS.Free();
    end;
    end;
    [/pascal]

    I hope it's clear now.

  3. #3
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,245
    Blog Entries
    2

    Re: Data files

    Brainer's solution will work, if you are using streams... but you're using standard Pascal file handling from what I can see, so this is how you do it...

    When you create the file, you open it with 'rewrite'. This creates the file everytime. When you want to edit a record or just read the data in the file, you open the file with 'reset' and then use 'seek' to find the record you want based on it's record number.

    There are other routines which you may find useful... 'filepos' returns the current record number, 'filesize' returns the total number of records in the file. Record indices start at 0 for the first record.

    When you want to edit a record, you use 'seek' to get to the record you want to edit, then read it using 'read'. Make the changes to the record in memory, then move back to it (remember 'read' advances the files record pointer) using 'seek' and then simply 'write' the record back.

    So, the first time you open the file use 'rewrite' to create it. The next time, check if it exists using 'fileexists' and if it does, open it with 'reset'. If you want to add a new record, remember to go to the end of the file using something like 'seek(f,filesize(f))'.

    These are the standard file handling commands for real Pascal files... there are others but these are for specific file types ('append' for example is only for use with text files).
    :: AthenaOfDelphi :: My Blog :: My Software ::

  4. #4

    Re: Data files

    Doh, I worked it out.
    Thanks.

    But now I have a different problem.
    I'm using crt and gotoxy to format the view better.
    My question is - can I somehow define the size of the window in which the program opens?

    When I run it on my computer, it looks fine, but when my friend runs it on his computer, the screen is smaller, so everything kind of scrambles...

    EDIT:
    Solved that through windows.

  5. #5

    Re: Data files

    Quote Originally Posted by Ixy
    EDIT:
    Solved that through windows.
    how ? I had this problem on the past but i wasn't able to solve it =\
    From brazil (:

    Pascal pownz!

  6. #6

    Re: Data files

    Well, you open the exe file and it runs in a pretty small window (at least in win xp). Then right click on the border of the window -> properties -> layout. And change the settings there.

    Now I have a new problem. For some reason, everytime i want to write something to a file, I get exitcode 5.
    Which is "File access denied."

  7. #7

    Re: Data files

    Don't know if this will help - exit code 5 is a file I/O access error, try and change the filemode
    Wake up from the dream and live your life to the full

  8. #8

    Re: Data files

    Quote Originally Posted by Ixy
    Well, you open the exe file and it runs in a pretty small window (at least in win xp). Then right click on the border of the window -> properties -> layout. And change the settings there.

    Now I have a new problem. For some reason, everytime i want to write something to a file, I get exitcode 5.
    Which is "File access denied."
    uhm, but how about changing the console size in the program?
    From brazil (:

    Pascal pownz!

  9. #9

    Re: Data files

    Quote Originally Posted by arthurprs
    uhm, but how about changing the console size in the program?
    Is it same console than OS opens when you start command prompt? In that case above instruction would make sense.

  10. #10

    Re: Data files

    Quote Originally Posted by Ixy
    Now I have a new problem. For some reason, everytime i want to write something to a file, I get exitcode 5.
    Which is "File access denied."
    Maybe something useful here:
    http://www.delphibasics.co.uk/RTL.asp?Name=Reset
    "Reset - Open a text file for reading, or binary file for read/write"

    Make sure you use Write, not Writeln? If doesn't work we might need to see the use of these commands in your program.

    Also "myFile : TextFile;" < this is not allowed as you need a binary file.
    http://www.delphibasics.co.uk/RTL.asp?Name=Write

    I have actually never tried using dynamic block sizes with this method. Is it possible?

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •