Recent

Author Topic: Access Violation Reading File [SOLVED]  (Read 4412 times)

nugax

  • Full Member
  • ***
  • Posts: 232
Access Violation Reading File [SOLVED]
« 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;
« Last Edit: February 14, 2018, 10:21:03 pm by nugax »
-Nugax

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Access Violation Reading File
« Reply #1 on: February 12, 2018, 10:32:30 pm »
try
Code: Pascal  [Select][+][-]
  1.   blockread(AnsiFile, buffer[1], FileSize(AnsiFile));
  2.  
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

nugax

  • Full Member
  • ***
  • Posts: 232
Re: Access Violation Reading File
« Reply #2 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.  

-Nugax

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Access Violation Reading File
« Reply #3 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.


The only true wisdom is knowing you know nothing

nugax

  • Full Member
  • ***
  • Posts: 232
Re: Access Violation Reading File
« Reply #4 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.
-Nugax

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Access Violation Reading File
« Reply #5 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.  
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

nugax

  • Full Member
  • ***
  • Posts: 232
Re: Access Violation Reading File
« Reply #6 on: February 12, 2018, 11:28:14 pm »
Worked, thanks!
-Nugax

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Access Violation Reading File
« Reply #7 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;  
« Last Edit: February 12, 2018, 11:51:47 pm by howardpc »

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Access Violation Reading File
« Reply #8 on: February 13, 2018, 10:16:04 am »
This is a text file isn't it?
Why not use TStringList.LoadFromFile then?

Bart

nugax

  • Full Member
  • ***
  • Posts: 232
Re: Access Violation Reading File
« Reply #9 on: February 13, 2018, 04:39:27 pm »
NO its an ANSI file
-Nugax

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Access Violation Reading File
« Reply #10 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

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: Access Violation Reading File
« Reply #11 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, so not doing that on your own isn't necessarily wrong.
Also, InOutRes is documented as read-only, so you aren't really supposed to alter that value directly but via IOResult().

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.  
Yours Sincerely
Kai Burghardt

nugax

  • Full Member
  • ***
  • Posts: 232
Re: Access Violation Reading File [SOLVED]
« Reply #12 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
-Nugax

 

TinyPortal © 2005-2018