Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

My program cannot read a file!!!

I would appreciate it very much if someone could help me with this one!!

I have made an application that (besides of everything else) reads
number from a file named "Rithmos". The command that reads the file
is: "Read(Rithmos,Box16);" . Τhe problem is that in the second
execution of this command the following message appears:

"Project PROJECT1.exe raised exception class EInOutError with
message "I/O error 6".Process stopped. Use Step or Run to continue."

Rithmos is defined as "Rithmos : FILE Of Word;" and Box16 is a Word-type
variable!! The file associated with Rithmos is 182KB so there is no
thought of reaching the end of file in two execution!!

What is sure is that the error is caused due to the Read command (i
have checked this out),but i cannot find a reason!!??!!

Another clue is that i have tried to read the file (with another
program that i have created) as a TFileStream , reading two bytes
a time and storing these two bytes in a Word variable and there is
no problem!! When i try to do the same (Reading the file as a
TFileStream) in my first program it fails, raising a EAccessViolation
exception!!

Any idea??

*I am very desperated because this program belongs in the last part
of my final project in my school (which i have to present next month)
in order to get my degree!! My application is available if someone have
the time to examine it more closely (it is not very large).

Comments

  • softmansoftman Posts: 157Member
    Hi

    For better understanding I should see, how you assigned the file to the variable. But it seems to me, that the file is closed before the second execution of the read command.


    : I would appreciate it very much if someone could help me with this one!!
    :
    : I have made an application that (besides of everything else) reads
    : number from a file named "Rithmos". The command that reads the file
    : is: "Read(Rithmos,Box16);" . Τhe problem is that in the second
    : execution of this command the following message appears:
    :
    : "Project PROJECT1.exe raised exception class EInOutError with
    : message "I/O error 6".Process stopped. Use Step or Run to continue."
    :
    : Rithmos is defined as "Rithmos : FILE Of Word;" and Box16 is a Word-type
    : variable!! The file associated with Rithmos is 182KB so there is no
    : thought of reaching the end of file in two execution!!
    :
    : What is sure is that the error is caused due to the Read command (i
    : have checked this out),but i cannot find a reason!!??!!
    :
    : Another clue is that i have tried to read the file (with another
    : program that i have created) as a TFileStream , reading two bytes
    : a time and storing these two bytes in a Word variable and there is
    : no problem!! When i try to do the same (Reading the file as a
    : TFileStream) in my first program it fails, raising a EAccessViolation
    : exception!!
    :
    : Any idea??
    :
    : *I am very desperated because this program belongs in the last part
    : of my final project in my school (which i have to present next month)
    : in order to get my degree!! My application is available if someone have
    : the time to examine it more closely (it is not very large).
    :

  • DonAlexandroDonAlexandro Posts: 32Member
    > The assignment command is (the usual one):
    > "AssignFile(Rithmos,RateName); -> RateName is a String variable
    > with the name(+location) of the file
    > After this command "Reset(Rithmos);" follows.
    > I use Delphi 2.
    > I would check the state (Open/Closed) of the file before the
    > execution just for the case...


    : Hi
    :
    : For better understanding I should see, how you assigned the file to the variable. But it seems to me, that the file is closed before the second execution of the read command.
    :

  • softmansoftman Posts: 157Member
    Hi!

    For me, it seems OK. I'v tried the code you wrote, and for me it works fine, but I use delphi 5.

    : > The assignment command is (the usual one):
    : > "AssignFile(Rithmos,RateName); -> RateName is a String variable
    : > with the name(+location) of the file
    : > After this command "Reset(Rithmos);" follows.
    : > I use Delphi 2.
    : > I would check the state (Open/Closed) of the file before the
    : > execution just for the case...
    :
    :
    : : Hi
    : :
    : : For better understanding I should see, how you assigned the file to the variable. But it seems to me, that the file is closed before the second execution of the read command.
    : :
    :
    :

  • zibadianzibadian Posts: 6,349Member
    According to the help I/O Error 6 is an invalid handle, but that would mean that after the read() statement the file variable suddenly becomes invalid/closed or something like that. If you post the actual code you're using, perhaps some-one here can find the error.
    There is also the possibility that D2 has some internal bug concerning the file-io, but I doubt it, becuase D1, D3 & D5 didn't have them.

    : Hi!
    :
    : For me, it seems OK. I'v tried the code you wrote, and for me it works fine, but I use delphi 5.
    :
    : : > The assignment command is (the usual one):
    : : > "AssignFile(Rithmos,RateName); -> RateName is a String variable
    : : > with the name(+location) of the file
    : : > After this command "Reset(Rithmos);" follows.
    : : > I use Delphi 2.
    : : > I would check the state (Open/Closed) of the file before the
    : : > execution just for the case...
    : :
    : :
    : : : Hi
    : : :
    : : : For better understanding I should see, how you assigned the file to the variable. But it seems to me, that the file is closed before the second execution of the read command.
    : : :
    : :
    : :
    :
    :

  • DonAlexandroDonAlexandro Posts: 32Member
    Being an Run-Time Error i assume that 6 refers to 106 because Delphi>Help says:

    The run-time errors are divided into three categories:

    I/O errors, numbered 100 through 149
    fatal errors, numbered 200 through 255
    Operating system errors

    So looking for error 106 i found :

    Number Name Description
    106 Invalid numeric format -> Reported by Read or Readln if
    a numeric value read from a
    text file does not conform to
    the proper numeric format.

    The main part of the code is following:

    var
    Form1: TForm1;

    implementation

    {$R *.DFM}
    Const
    N = 8;
    Groups = 8;
    Window = 0.016;
    a = 0.95;

    Var
    BigGroup,CurGroup : Byte;
    Box16,AvailBits,BitsLeft,max,DiffMax : Word;
    I,J,Count,k,DataFileSize,Repeats : DWord;
    Sample,LastOne : SmallInt;
    Variance,Estimation,Diff : Single;
    SourceName,RateName : String;
    Frame : ARRAY[1..N*Groups] Of SmallInt;
    Vari,Ri : ARRAY[1..Groups] Of Single;
    Rithmos : FILE Of Word;
    Source,Target : FILE Of SmallInt;

    procedure TForm1.EXIT1Click(Sender: TObject);
    begin
    Application.Terminate;
    end;

    procedure TForm1.Button3Click(Sender: TObject);
    begin
    SourceName:=Edit1.Text;
    RateName:=Edit2.Text;
    AssignFile(Source,SourceName);
    Reset(Source);
    Read(Source,LastOne);
    CloseFile(Source);
    AssignFile(Source,SourceName);
    Reset(Source);
    AssignFile(Rithmos,RateName);
    Reset(Rithmos);
    AssignFile(Target,'Target.q');
    Rewrite(Target);

    DataFileSize:=FileSize(Source);
    max:=32768;
    Repeats:=(DataFileSize div (N*Groups));
    DiffMax:=16384;
    For Count:=1 To Repeats Do
    Begin
    For I:=1 To N*Groups Do
    Begin
    Read(Source,Sample);
    Frame[I]:=Sample;
    End;
    Variance:=0;
    For I:=1 To Groups Do Vari[I]:=0;
    For I:=1 To N*Groups Do
    Begin
    Variance:=Variance+(Frame[I]/max)*(Frame[I]/max);
    CurGroup:=((I-1) div N)+1;
    Vari[CurGroup]:=Vari[CurGroup]+(Frame[I]/max)*(Frame[I]/max);
    End;
    For I:=1 To Groups Do Vari[I]:=Vari[I]/Variance;
    Read(Rithmos,Box16);
    AvailBits:=Box16;
    For I:=1 To Groups Do Ri[I]:=Vari[I]*AvailBits;
    BitsLeft:=AvailBits;
    For J:=1 To Groups Do
    Begin
    If BitsLeft=0 Then
    Begin
    BigGroup:=1;
    For k:=2 To Groups Do
    If Vari[I]>Vari[BigGroup] Then BigGroup:=k;
    Ri[BigGroup]:=Ri[BigGroup]-N;
    BitsLeft:=BitsLeft+N;
    End;
    If (((Ri[I]-(Trunc(Ri[I]/N)*N))/N)>=0.75) OR (Ri[I]=DiffMax Then Diff:=DiffMax-1
    else If Diff<-DiffMax Then Diff:=-DiffMax;
    Diff:=Diff+DiffMax;
    CurGroup:=((I-1) div N)+1;
    Diff:=(Diff/(2*DiffMax))*exp(Round(Ri[CurGroup]/N)*Ln(2));
    Diff:=Trunc(Diff);
    Diff:=Diff*2*DiffMax/exp(Round(Ri[CurGroup]/N)*Ln(2));
    Diff:=Diff-DiffMax;
    Frame[I]:=Frame[I-1]+Round((Diff)
    End;
    LastOne:=Frame[N*Groups];

    For I:=1 To N*Groups Do Write(Target,Frame[I]);
    End;

    CloseFile(Source);
    CloseFile(Target);
    CloseFile(Rithmos);
    end;


    The messageboard doesn't show my code very well but i can't do
    something about that!! If anyone want to send him my code through e-mail
    just tell me. The problematic command is the Read(Rithmos,Box16)!!

    I have checked it more and i found that the problem starts just after the first execution of this command!? After the first execution , which
    is succesed because the value of IOResult returned if i put this command in a {$I-} {$I+} block is 0, i cannot even execute an Eof(Rithmos) command!! The access to this file becomes problematic!!

    HELP..!!!!
  • zibadianzibadian Posts: 6,349Member
    : Being an Run-Time Error i assume that 6 refers to 106 because Delphi>Help says:
    :
    : The run-time errors are divided into three categories:
    :
    : I/O errors, numbered 100 through 149
    : fatal errors, numbered 200 through 255
    : Operating system errors
    :
    : So looking for error 106 i found :
    :
    : Number Name Description
    : 106 Invalid numeric format -> Reported by Read or Readln if
    : a numeric value read from a
    : text file does not conform to
    : the proper numeric format.
    :
    : The main part of the code is following:
    :
    : var
    : Form1: TForm1;
    :
    : implementation
    :
    : {$R *.DFM}
    : Const
    : N = 8;
    : Groups = 8;
    : Window = 0.016;
    : a = 0.95;
    :
    : Var
    : BigGroup,CurGroup : Byte;
    : Box16,AvailBits,BitsLeft,max,DiffMax : Word;
    : I,J,Count,k,DataFileSize,Repeats : DWord;
    : Sample,LastOne : SmallInt;
    : Variance,Estimation,Diff : Single;
    : SourceName,RateName : String;
    : Frame : ARRAY[1..N*Groups] Of SmallInt;
    : Vari,Ri : ARRAY[1..Groups] Of Single;
    : Rithmos : FILE Of Word;
    : Source,Target : FILE Of SmallInt;
    :
    : procedure TForm1.EXIT1Click(Sender: TObject);
    : begin
    : Application.Terminate;
    : end;
    :
    : procedure TForm1.Button3Click(Sender: TObject);
    : begin
    : SourceName:=Edit1.Text;
    : RateName:=Edit2.Text;
    : AssignFile(Source,SourceName);
    : Reset(Source);
    : Read(Source,LastOne);
    : CloseFile(Source);
    : AssignFile(Source,SourceName);
    : Reset(Source);
    : AssignFile(Rithmos,RateName);
    : Reset(Rithmos);
    : AssignFile(Target,'Target.q');
    : Rewrite(Target);
    :
    : DataFileSize:=FileSize(Source);
    : max:=32768;
    : Repeats:=(DataFileSize div (N*Groups));
    : DiffMax:=16384;
    : For Count:=1 To Repeats Do
    : Begin
    : For I:=1 To N*Groups Do
    : Begin
    : Read(Source,Sample);
    : Frame[I]:=Sample;
    : End;
    : Variance:=0;
    : For I:=1 To Groups Do Vari[I]:=0;
    : For I:=1 To N*Groups Do
    : Begin
    : Variance:=Variance+(Frame[I]/max)*(Frame[I]/max);
    : CurGroup:=((I-1) div N)+1;
    : Vari[CurGroup]:=Vari[CurGroup]+(Frame[I]/max)*(Frame[I]/max);
    : End;
    : For I:=1 To Groups Do Vari[I]:=Vari[I]/Variance;
    : Read(Rithmos,Box16);
    : AvailBits:=Box16;
    : For I:=1 To Groups Do Ri[I]:=Vari[I]*AvailBits;
    : BitsLeft:=AvailBits;
    : For J:=1 To Groups Do
    : Begin
    : If BitsLeft=0 Then
    : Begin
    : BigGroup:=1;
    : For k:=2 To Groups Do
    : If Vari[I]>Vari[BigGroup] Then BigGroup:=k;
    : Ri[BigGroup]:=Ri[BigGroup]-N;
    : BitsLeft:=BitsLeft+N;
    : End;
    : If (((Ri[I]-(Trunc(Ri[I]/N)*N))/N)>=0.75) OR (Ri[I]=DiffMax Then Diff:=DiffMax-1
    : else If Diff<-DiffMax Then Diff:=-DiffMax;
    : Diff:=Diff+DiffMax;
    : CurGroup:=((I-1) div N)+1;
    : Diff:=(Diff/(2*DiffMax))*exp(Round(Ri[CurGroup]/N)*Ln(2));
    : Diff:=Trunc(Diff);
    : Diff:=Diff*2*DiffMax/exp(Round(Ri[CurGroup]/N)*Ln(2));
    : Diff:=Diff-DiffMax;
    : Frame[I]:=Frame[I-1]+Round((Diff)
    : End;
    : LastOne:=Frame[N*Groups];
    :
    : For I:=1 To N*Groups Do Write(Target,Frame[I]);
    : End;
    :
    : CloseFile(Source);
    : CloseFile(Target);
    : CloseFile(Rithmos);
    : end;
    :
    :
    : The messageboard doesn't show my code very well but i can't do
    : something about that!! If anyone want to send him my code through e-mail
    : just tell me. The problematic command is the Read(Rithmos,Box16)!!
    :
    : I have checked it more and i found that the problem starts just after the first execution of this command!? After the first execution , which
    : is succesed because the value of IOResult returned if i put this command in a {$I-} {$I+} block is 0, i cannot even execute an Eof(Rithmos) command!! The access to this file becomes problematic!!
    :
    : HELP..!!!!
    :
    You can show code exactly like you want to, if you use stylecodes in your reply (see under the reply box for instructions).
    Also according to the windows SDK there is an I/O Error 6, which is generated by the os itself, if there is an invalid handle.
    Also the I/O error 106 is only generated if the filetype is a textfile. Otherwise Delphi will automatically assume that the file consists of the types supplied, and will simply (in you case) read the first 2 bytes of the file and type-cast them as a word.
    What was the IOResult() during the second execution?
    I cannot see what could be wrong with the code, so I would ask you to send me the entire project and the datafiles. Then I can step through them to see what is wrong.
  • DonAlexandroDonAlexandro Posts: 32Member
    IOResult in second execution is 6 (if i remember well).

    I will send you my application plus the data files
    through e-mail if its OK (but tommorow because i dont
    have a fast enough internet connection at my home )!

    What makes me really angry is that accessing the datafile
    through TFileStream is working in my other application but
    not in this (when i tried it)!!*#@$! :-((

  • DonAlexandroDonAlexandro Posts: 32Member
    OK! I found it!! :-))
    Delphi has not a bug, but the programmer (me!) has one!! :-)
    I was making an illegal use of some variables that were used
    as array indexers and it seems that this was resulting in writing to unathurized areas (e.g the file area).

    Thanks, for your replys!!

    *Zibadian if you are still looking or you intend to look for the bug
    then don't!!
Sign In or Register to comment.