Lazarus

Programming => General => Topic started by: nugax on February 12, 2018, 10:27:11 pm

Title: Access Violation Reading File [SOLVED]
Post by: nugax on February 12, 2018, 10:27:11 pm
Hi Guys & Gals,

I have the following code, but I get an access violation on the blockread. Anyone know why or a better way to read in a file into a buffer?
Its an ansi file I want to then write out.

Code: Pascal  [Select][+][-]
  1. procedure displayansi(var sFileName: string);
  2.  
  3. Var
  4.   buffer : ansistring;
  5.   iCounter : integer;
  6.   AnsiFile : file;
  7.  
  8. Begin
  9.   Assign (AnsiFile, sFileName);
  10.   Reset(AnsiFile, 1);
  11.   buffer := '';
  12.   setLength(buffer, filesize(AnsiFile));
  13.   blockread(AnsiFile, buffer, FileSize(AnsiFile));
  14.   close(AnsiFile);
  15.  
  16.  
  17. End;
Title: Re: Access Violation Reading File
Post by: taazz on February 12, 2018, 10:32:30 pm
try
Code: Pascal  [Select][+][-]
  1.   blockread(AnsiFile, buffer[1], FileSize(AnsiFile));
  2.  
Title: Re: Access Violation Reading File
Post by: nugax on February 12, 2018, 10:49:32 pm
That fixed the error reading. Now I get an error saying file not opened.

Code: Pascal  [Select][+][-]
  1. procedure displayansi(var sFileName: string);
  2.  
  3. Var
  4.   buffer : ansistring;
  5.   iCounter : integer;
  6.   AnsiFile : file;
  7.  
  8. Begin
  9.   Assign (AnsiFile, sFileName);
  10.   Reset(AnsiFile, 1);
  11.   buffer := '';
  12.   setLength(buffer, filesize(AnsiFile));
  13.   blockread(AnsiFile, buffer[1], FileSize(AnsiFile));
  14.   close(AnsiFile);
  15.  
  16.   //Write File
  17.   for iCounter :=0 to filesize(ansifile) do
  18.     write(buffer[iCounter]);
  19.  
  20.  
  21. End;  


try
Code: Pascal  [Select][+][-]
  1.   blockread(AnsiFile, buffer[1], FileSize(AnsiFile));
  2.  

Title: Re: Access Violation Reading File
Post by: jamie on February 12, 2018, 11:06:43 pm
The file is not open sounds about right...

Save the FileSize in a variable when you open the file the first time so then you use that
reference later...

 Also, you already set the size of the String at the start so you can use that instead.

 If the fileSize fails after you have opened it, it a good chance there is something wrong with the
file, like maybe it does not exist..?

 before doing any pascal IO, first do this

 InOutRes := 0;

 This will clear any pending errors that maybe hanging in limbo .

 Previous errors will prevent you from doing any more IO..

So,

 InOutRes := 0;
 AssignFile(......);
 Reset(.....)
 if IoResult <> 0 Then Something IS Wrong, like maybe the file does not exist.


Title: Re: Access Violation Reading File
Post by: nugax on February 12, 2018, 11:18:42 pm
Its not opening as you said. It does error on the check:

if IoResult <> 0 Then Something IS Wrong, like maybe the file does not exist.

But, the file does exist and has permissions of 777.
And I can cat the file (Linux) and display it fine.
Title: Re: Access Violation Reading File
Post by: taazz on February 12, 2018, 11:25:37 pm
That fixed the error reading. Now I get an error saying file not opened.
When? when you try to print the buffer on screen? before that? You do know that you call close(AnsiFile); and then you try filesize(ansifile) how about changing the code to
Code: Pascal  [Select][+][-]
  1.     procedure displayansi(var sFileName: string);
  2.      
  3.     Var
  4.       buffer : ansistring;
  5.       iCounter : integer;
  6.       AnsiFile : file;
  7.      
  8.     Begin
  9.       Assign (AnsiFile, sFileName);
  10.       Reset(AnsiFile, 1);
  11.       buffer := '';
  12.       setLength(buffer, filesize(AnsiFile));
  13.       blockread(AnsiFile, buffer[1], FileSize(AnsiFile));
  14.       close(AnsiFile);
  15.      
  16.       //Write File
  17.       for iCounter :=0 to Length(Buffer) do //<--------major changes.
  18.         write(buffer[iCounter]);
  19.      
  20.      
  21.     End;  
  22.  
Title: Re: Access Violation Reading File
Post by: nugax on February 12, 2018, 11:28:14 pm
Worked, thanks!
Title: Re: Access Violation Reading File
Post by: howardpc on February 12, 2018, 11:48:55 pm
I think this would be better:
Code: Pascal  [Select][+][-]
  1. procedure DisplayAnsi(const sFileName: String);
  2. var
  3.   sBuffer:   String;
  4.   fAnsiFile: File;
  5.   c:         Char;
  6. begin
  7.   if FileExists(sFileName) then
  8.   begin
  9.     AssignFile(fAnsiFile, sFileName);
  10.     try
  11.       Reset(fAnsiFile, 1);
  12.       SetLength(sbuffer, FileSize(fAnsiFile));
  13.       BlockRead(fAnsiFile, sBuffer[1], Length(sBuffer));
  14.     finally
  15.       CloseFile(fAnsiFile);
  16.     end;
  17.     for c in sBuffer do
  18.       Write(c);
  19.   end;
  20. end;  
Title: Re: Access Violation Reading File
Post by: Bart on February 13, 2018, 10:16:04 am
This is a text file isn't it?
Why not use TStringList.LoadFromFile then?

Bart
Title: Re: Access Violation Reading File
Post by: nugax on February 13, 2018, 04:39:27 pm
NO its an ANSI file
Title: Re: Access Violation Reading File
Post by: Bart on February 13, 2018, 05:09:51 pm
NO its an ANSI file

What do you mean by that?
I ask, since you seem to want to load the contents in a String.

Bart
Title: Re: Access Violation Reading File
Post by: Kays on February 13, 2018, 06:05:24 pm
[…]
 before doing any pascal IO, first do this

 InOutRes := 0;
[…]
InOutRes is reset by the system unit (https://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/rtl/linux/system.pp?revision=36695&view=markup#l452), so not doing that on your own isn't necessarily wrong.
Also, InOutRes is documented as read-only (https://freepascal.org/docs-html/current/rtl/system/inoutres.html), so you aren't really supposed to alter that value directly but via IOResult() (https://freepascal.org/docs-html/current/rtl/system/ioresult.html).

Code: Pascal  [Select][+][-]
  1.   { Arguments }
  2.   SysInitExecPath;
  3.   { Reset IO Error }
  4.   InOutRes:=0;
  5.   { threading }
  6.   InitSystemThreads;
  7.   { restore original signal handlers in case this is a library }
  8.  
Title: Re: Access Violation Reading File [SOLVED]
Post by: nugax on February 14, 2018, 10:20:29 pm
Well, I guess yes its a text file. And I needed to write out char by char. I got it working! Thanks for the help!

NO its an ANSI file

What do you mean by that?
I ask, since you seem to want to load the contents in a String.

Bart
TinyPortal © 2005-2018