Howdy, Stranger!

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

Categories

Write to a typed file

JoseCJoseC Member Posts: 17
I want to make an end of line after some text in a typed file, I think it's a typed file because I am writing something from a [b]record[/b] to it. My program just writes everything in one single line, which doesn't look very nice.

And I also wonder if you can get rid of those ugly squares that Delphi writes between different values in the file.

Comments

  • zibadianzibadian Member Posts: 6,349
    : I want to make an end of line after some text in a typed file, I think it's a typed file because I am writing something from a [b]record[/b] to it. My program just writes everything in one single line, which doesn't look very nice.
    :
    : And I also wonder if you can get rid of those ugly squares that Delphi writes between different values in the file.
    :
    Writing something from a record to a file doesn't necessarily mean that the file is a typed file. You can see, if a file is a typed file from its declaration. If it is declared as something like this:
    [code]
    var
    f: file of SomeRecord;

    // OR

    type
    TMyFile = file of SomeRecord;
    var
    f: TMyFile;
    [/code]
    then it's a typed file. Otherwise its either a text-file ([b]textfile[/b] type) or an untyped file (only the word [b]file[/b] as type). I don't think it's an untyped file, since then you cannot use the Write() procedure to write data to it.
    In case of a textfile you can use the Writeln() procedure to automatically add line endings. As for typed files, you cannot add line endings to it, because that would mean that the record-alignment inside the file changes and the file becomes corrupt.
  • JoseCJoseC Member Posts: 17
    : Writing something from a record to a file doesn't necessarily mean that the file is a typed file. You can see, if a file is a typed file from its declaration. If it is declared as something like this:
    : [code]
    : var
    : f: file of SomeRecord;
    :
    : // OR
    :
    : type
    : TMyFile = file of SomeRecord;
    : var
    : f: TMyFile;
    : [/code]
    : then it's a typed file. Otherwise its either a text-file ([b]textfile[/b] type) or an untyped file (only the word [b]file[/b] as type). I don't think it's an untyped file, since then you cannot use the Write() procedure to write data to it.
    : In case of a textfile you can use the Writeln() procedure to automatically add line endings. As for typed files, you cannot add line endings to it, because that would mean that the record-alignment inside the file changes and the file becomes corrupt.
    :
    My code looks like this:
    [code]
    [b]type[/b]
    TimeandCost = [b]record[/b]
    Date: [b]String[/b][10];
    Hours: [b]String[/b][2];
    Minutes: [b]String[/b][2];
    Seconds: [b]String[/b][2];
    [b]end[/b];

    [b]var[/b]
    frmMain: TfrmMain;
    Statistic: file of TimeandCost;
    TimeCost: TimeandCost;

    //implementation
    //...

    [b]procedure[/b] TfrmMain.FormDestroy(Sender: TObject);
    [b]begin[/b]
    Seek(Statistic, FileSize(Statistic));
    TimeCost.Date := DateToStr(Now);
    TimeCost.Hours := lblHour.Caption;
    TimeCost.Minutes := lblMinute.Caption;
    TimeCost.Seconds := lblSecond.Caption;
    Write(Statistic, TimeCost);
    [b]end[/b];
    [/code]
    Is it a typed file? I couldn't change Write to Writeln, so how can I do a text file instead but keep my TimeAndCost record.
  • zibadianzibadian Member Posts: 6,349
    : : Writing something from a record to a file doesn't necessarily mean that the file is a typed file. You can see, if a file is a typed file from its declaration. If it is declared as something like this:
    : : [code]
    : : var
    : : f: file of SomeRecord;
    : :
    : : // OR
    : :
    : : type
    : : TMyFile = file of SomeRecord;
    : : var
    : : f: TMyFile;
    : : [/code]
    : : then it's a typed file. Otherwise its either a text-file ([b]textfile[/b] type) or an untyped file (only the word [b]file[/b] as type). I don't think it's an untyped file, since then you cannot use the Write() procedure to write data to it.
    : : In case of a textfile you can use the Writeln() procedure to automatically add line endings. As for typed files, you cannot add line endings to it, because that would mean that the record-alignment inside the file changes and the file becomes corrupt.
    : :
    : My code looks like this:
    : [code]
    : [b]type[/b]
    : TimeandCost = [b]record[/b]
    : Date: [b]String[/b][10];
    : Hours: [b]String[/b][2];
    : Minutes: [b]String[/b][2];
    : Seconds: [b]String[/b][2];
    : [b]end[/b];
    :
    : [b]var[/b]
    : frmMain: TfrmMain;
    : Statistic: file of TimeandCost;
    : TimeCost: TimeandCost;
    :
    : //implementation
    : //...
    :
    : [b]procedure[/b] TfrmMain.FormDestroy(Sender: TObject);
    : [b]begin[/b]
    : Seek(Statistic, FileSize(Statistic));
    : TimeCost.Date := DateToStr(Now);
    : TimeCost.Hours := lblHour.Caption;
    : TimeCost.Minutes := lblMinute.Caption;
    : TimeCost.Seconds := lblSecond.Caption;
    : Write(Statistic, TimeCost);
    : [b]end[/b];
    : [/code]
    : Is it a typed file? I couldn't change Write to Writeln, so how can I do a text file instead but keep my TimeAndCost record.
    :
    It's a typed file, since the declaration uses the words [b]file of[/b]. You can use a text file in this case, although reading that file in a program will require some string-handling. Here is a code to write the data to a textfile:
    : [code]
    : [b]type[/b]
    : TimeandCost = [b]record[/b]
    : Date: [b]String[/b][10];
    : Hours: [b]String[/b][2];
    : Minutes: [b]String[/b][2];
    : Seconds: [b]String[/b][2];
    : [b]end[/b];
    :
    : [b]var[/b]
    : frmMain: TfrmMain;
    : Statistic: textfile;
    : TimeCost: TimeandCost;
    :
    : //implementation
    : //...
    :
    : [b]procedure[/b] TfrmMain.FormDestroy(Sender: TObject);
    : [b]begin[/b]
    : Seek(Statistic, FileSize(Statistic));
    : TimeCost.Date := DateToStr(Now);
    : TimeCost.Hours := lblHour.Caption;
    : TimeCost.Minutes := lblMinute.Caption;
    : TimeCost.Seconds := lblSecond.Caption;
    with TimeCost do
    Writeln(Statistic, Date, '|', Hours, '|', Minutes, '|', Seconds);
    : [b]end[/b];
    : [/code]
    This will create the output as follows:
    [code]
    01-24-2004|14|59|12
    01-23-2004|5|17|11
    [/code]
    In this case you cannot use [b]read(Statistic, TimeCOst)[/b] anymore, since that would probably give an compiler error. If you want to read it, you'll need to read the entire line in a temporary string and split it in parts afterwards. Here is a function for that: http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=4&MsgID=188161
  • JoseCJoseC Member Posts: 17
    : It's a typed file, since the declaration uses the words [b]file of[/b]. You can use a text file in this case, although reading that file in a program will require some string-handling. Here is a code to write the data to a textfile:
    : : [code]
    : : [b]type[/b]
    : : TimeandCost = [b]record[/b]
    : : Date: [b]String[/b][10];
    : : Hours: [b]String[/b][2];
    : : Minutes: [b]String[/b][2];
    : : Seconds: [b]String[/b][2];
    : : [b]end[/b];
    : :
    : : [b]var[/b]
    : : frmMain: TfrmMain;
    : : Statistic: textfile;
    : : TimeCost: TimeandCost;
    : :
    : : //implementation
    : : //...
    : :
    : : [b]procedure[/b] TfrmMain.FormDestroy(Sender: TObject);
    : : [b]begin[/b]
    : : Seek(Statistic, FileSize(Statistic));
    : : TimeCost.Date := DateToStr(Now);
    : : TimeCost.Hours := lblHour.Caption;
    : : TimeCost.Minutes := lblMinute.Caption;
    : : TimeCost.Seconds := lblSecond.Caption;
    : with TimeCost do
    : Writeln(Statistic, Date, '|', Hours, '|', Minutes, '|', Seconds);
    : : [b]end[/b];
    : : [/code]
    : This will create the output as follows:
    : [code]
    : 01-24-2004|14|59|12
    : 01-23-2004|5|17|11
    : [/code]
    : In this case you cannot use [b]read(Statistic, TimeCOst)[/b] anymore, since that would probably give an compiler error. If you want to read it, you'll need to read the entire line in a temporary string and split it in parts afterwards. Here is a function for that: http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=4&MsgID=188161
    :
    I followed the code example and the program worked, but when I closed the program I got an EInOutError(I/O error 103) on the Writeln line.
  • JoseCJoseC Member Posts: 17
    : : It's a typed file, since the declaration uses the words [b]file of[/b]. You can use a text file in this case, although reading that file in a program will require some string-handling. Here is a code to write the data to a textfile:
    : : : [code]
    : : : [b]type[/b]
    : : : TimeandCost = [b]record[/b]
    : : : Date: [b]String[/b][10];
    : : : Hours: [b]String[/b][2];
    : : : Minutes: [b]String[/b][2];
    : : : Seconds: [b]String[/b][2];
    : : : [b]end[/b];
    : : :
    : : : [b]var[/b]
    : : : frmMain: TfrmMain;
    : : : Statistic: textfile;
    : : : TimeCost: TimeandCost;
    : : :
    : : : //implementation
    : : : //...
    : : :
    : : : [b]procedure[/b] TfrmMain.FormDestroy(Sender: TObject);
    : : : [b]begin[/b]
    : : : Seek(Statistic, FileSize(Statistic));
    : : : TimeCost.Date := DateToStr(Now);
    : : : TimeCost.Hours := lblHour.Caption;
    : : : TimeCost.Minutes := lblMinute.Caption;
    : : : TimeCost.Seconds := lblSecond.Caption;
    : : with TimeCost do
    : : Writeln(Statistic, Date, '|', Hours, '|', Minutes, '|', Seconds);
    : : : [b]end[/b];
    : : : [/code]
    : : This will create the output as follows:
    : : [code]
    : : 01-24-2004|14|59|12
    : : 01-23-2004|5|17|11
    : : [/code]
    : : In this case you cannot use [b]read(Statistic, TimeCOst)[/b] anymore, since that would probably give an compiler error. If you want to read it, you'll need to read the entire line in a temporary string and split it in parts afterwards. Here is a function for that: http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=4&MsgID=188161
    : :
    : I followed the code example and the program worked, but when I closed the program I got an EInOutError(I/O error 103) on the Writeln line.
    :
    I think I know what's wrong now but I don't know what code to use to fix it. I used Reset(Statistic) to open my file and in Delphi help it says that when you open a textfile with Reset it becomes Read-Only. So how do I open a textfile that I can want to write to?
  • zibadianzibadian Member Posts: 6,349
    : : : It's a typed file, since the declaration uses the words [b]file of[/b]. You can use a text file in this case, although reading that file in a program will require some string-handling. Here is a code to write the data to a textfile:
    : : : : [code]
    : : : : [b]type[/b]
    : : : : TimeandCost = [b]record[/b]
    : : : : Date: [b]String[/b][10];
    : : : : Hours: [b]String[/b][2];
    : : : : Minutes: [b]String[/b][2];
    : : : : Seconds: [b]String[/b][2];
    : : : : [b]end[/b];
    : : : :
    : : : : [b]var[/b]
    : : : : frmMain: TfrmMain;
    : : : : Statistic: textfile;
    : : : : TimeCost: TimeandCost;
    : : : :
    : : : : //implementation
    : : : : //...
    : : : :
    : : : : [b]procedure[/b] TfrmMain.FormDestroy(Sender: TObject);
    : : : : [b]begin[/b]
    : : : : Seek(Statistic, FileSize(Statistic));
    : : : : TimeCost.Date := DateToStr(Now);
    : : : : TimeCost.Hours := lblHour.Caption;
    : : : : TimeCost.Minutes := lblMinute.Caption;
    : : : : TimeCost.Seconds := lblSecond.Caption;
    : : : with TimeCost do
    : : : Writeln(Statistic, Date, '|', Hours, '|', Minutes, '|', Seconds);
    : : : : [b]end[/b];
    : : : : [/code]
    : : : This will create the output as follows:
    : : : [code]
    : : : 01-24-2004|14|59|12
    : : : 01-23-2004|5|17|11
    : : : [/code]
    : : : In this case you cannot use [b]read(Statistic, TimeCOst)[/b] anymore, since that would probably give an compiler error. If you want to read it, you'll need to read the entire line in a temporary string and split it in parts afterwards. Here is a function for that: http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=4&MsgID=188161
    : : :
    : : I followed the code example and the program worked, but when I closed the program I got an EInOutError(I/O error 103) on the Writeln line.
    : :
    : I think I know what's wrong now but I don't know what code to use to fix it. I used Reset(Statistic) to open my file and in Delphi help it says that when you open a textfile with Reset it becomes Read-Only. So how do I open a textfile that I can want to write to?
    :
    Either Rewrite() to create a new file or Append() to append lines to an existing file.
Sign In or Register to comment.