* * *

Author Topic: problem with textfile reading  (Read 1948 times)

Gebony

  • Newbie
  • Posts: 3
problem with textfile reading
« on: February 23, 2018, 08:34:31 am »
I have written codes for reading from file but the program can't read from it and  have got empty screen.

please help me

 
« Last Edit: February 23, 2018, 08:39:42 am by Gebony »

Thaddy

  • Hero Member
  • *****
  • Posts: 6349
Re: problem with textfile reading
« Reply #1 on: February 23, 2018, 09:15:44 am »
Code: Pascal  [Select]
  1. {$ifdef fpc}{$mode delphi}{$H+}{$I-}{$endif}
  2. var
  3.   path:string = 'profiles.txt';
  4.   line:string;
  5.   f:Text;
  6.   i:integer;
  7. begin
  8.   Assign(f,path);
  9.   reset(f);
  10.   i:= IOResult; // check! https://www.freepascal.org/docs-html/current/user/userap4.html#x190-197000D
  11.   if i <> 0 then writeln(i) else // or better writeln(STDERR, i) but that may confuse beginners
  12.   while not eof(f) do
  13.   begin
  14.     readln(f, line);
  15.     writeln(line);
  16.   end;
  17.   close(f);
  18. end.

Or:
Code: Pascal  [Select]
  1. {$ifdef fpc}{$mode delphi}{$H+}{$I+}{$endif}
  2. uses sysutils;
  3. var
  4.   path:string = 'profiles.txt';
  5.   line:string;
  6.   f:Text;
  7. begin
  8.   Assign(f,path);
  9.   try
  10.     reset(f);
  11.     while not eof(f) do
  12.     begin
  13.       readln(f, line);
  14.       writeln(line);
  15.     end;
  16.     close(f);
  17.   except
  18.     On E:Exception do
  19.       writeln(E.Message); // or similar writeln(STDERR, E.Message); see above
  20.   end;
  21. end.

First example tests for errors in {$I-} state and reads IOResult.
Second example tests for errors using exceptions and uses {$I+} state and the sysutils unit.

There are many more options, like using if FileExists('profiles.txt' ), using a file stream, etc, but these examples are consistent with your own code.
« Last Edit: February 23, 2018, 10:41:59 am by Thaddy »
I am enjoining wine, not whine....

RAW

  • Hero Member
  • *****
  • Posts: 667
Re: problem with textfile reading
« Reply #2 on: February 24, 2018, 06:58:43 pm »
Here is an example with TFileStream as Thaddy already mentioned... works for me...  :)

Code: Pascal  [Select]
  1. Function LoadText(strFile: String; Out str: String): Boolean;
  2.    Var
  3.     fs    : TFileStream;
  4.     iBytes: Integer;
  5.   Begin
  6.    Try
  7.     Result:= False;
  8.      If FileExists(strFile)
  9.      Then
  10.       Begin
  11.        fs:= TFileStream.Create(strFile, fmOpenRead);
  12.         Try
  13.          SetLength(str, fs.Size);
  14.          iBytes:= fs.Read(str[1], fs.Size);
  15.           If iBytes = fs.Size
  16.           Then Result:= True;
  17.         Finally
  18.          fs.Free;
  19.         End;
  20.       End
  21.      Else str:= 'FileExists Failed';
  22.    Except
  23.     On E: Exception
  24.     Do str:= 'LoadText Failed'+sLineBreak+E.ClassName+sLineBreak+E.Message;
  25.    End;
  26.   End;

PRG
Code: Pascal  [Select]
  1. PROGRAM LoadText;
  2. {$MODE OBJFPC}{$H+}
  3.  
  4.  USES
  5.   Crt, Classes, SysUtils, Keyboard;
  6.  
  7.  VAR
  8.   booQuit: Boolean;
  9.   chKey  : Char;
  10.  
  11.   strFileName, strLoad: String;
  12.  
  13.  
  14.  Function KeyInput: Char;
  15.    Var
  16.     Key: TKeyEvent;
  17.   Begin
  18.    InitKeyboard;
  19.     Try
  20.      Key   := GetKeyEvent;
  21.      Key   := TranslateKeyEvent(Key);
  22.      Result:= GetKeyEventChar(Key);
  23.     Finally
  24.      DoneKeyboard;
  25.     End;
  26.   End;
  27.  
  28.  
  29.  Function LoadText(strFile: String; Out str: String): Boolean;
  30.    Var
  31.     fs    : TFileStream;
  32.     iBytes: Integer;
  33.   Begin
  34.    Try
  35.     Result:= False;
  36.      If FileExists(strFile)
  37.      Then
  38.       Begin
  39.        fs:= TFileStream.Create(strFile, fmOpenRead);
  40.         Try
  41.          SetLength(str, fs.Size);
  42.          iBytes:= fs.Read(str[1], fs.Size);
  43.           If iBytes = fs.Size
  44.           Then Result:= True;
  45.         Finally
  46.          fs.Free;
  47.         End;
  48.       End
  49.      Else str:= 'FileExists Failed';
  50.    Except
  51.     On E: Exception
  52.     Do str:= 'LoadText Failed'+sLineBreak+E.ClassName+sLineBreak+E.Message;
  53.    End;
  54.   End;
  55.  
  56.  
  57. BEGIN
  58.  While Not booQuit
  59.  Do
  60.   Begin
  61.    ClrScr;
  62.  
  63.    WriteLn('  LOAD TEXT FILE');
  64.    WriteLn('==================');
  65.    WriteLn('Enter A FileName:');
  66.  
  67.     ReadLn(strFileName);
  68.      If strFileName <> ''
  69.      Then
  70.       If LoadText(strFileName, strLoad)
  71.       Then WriteLn(sLineBreak+'SUCCESS !!!'+sLineBreak+
  72.                    '====='+sLineBreak+strLoad+sLineBreak+'=====')
  73.       Else WriteLn(sLineBreak+'FAILURE !!!'+sLineBreak+strLoad);
  74.  
  75.      WriteLn;
  76.      WriteLn;
  77.      WriteLn('EXIT : ESC');
  78.      WriteLn('AGAIN: ENTER');
  79.  
  80.      Repeat
  81.       chKey:= KeyInput;
  82.      Until (chKey = #27) Or (chKey = #13);
  83.  
  84.      If chKey = #27 Then Break;
  85.   End;
  86. END.
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 1.8.4 FPC 3.0.4 // 1.7 FPC 3.1.1

Zoran

  • Hero Member
  • *****
  • Posts: 1243
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: problem with textfile reading
« Reply #3 on: February 24, 2018, 09:11:24 pm »
Let's make it simple...

See this line:
Code: Pascal  [Select]
  1. while not EOF do begin
Here EOF is end of which file?

EOF needs parameter.
Should be:
Code: Pascal  [Select]
  1. while not EOF(f) do begin

Leledumbo

  • Hero Member
  • *****
  • Posts: 7927
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: problem with textfile reading
« Reply #4 on: February 25, 2018, 07:55:14 am »
Let's make it simple...

See this line:
Code: Pascal  [Select]
  1. while not EOF do begin
Here EOF is end of which file?

EOF needs parameter.
Should be:
Code: Pascal  [Select]
  1. while not EOF(f) do begin
Just to correct: EOF doesn't need parameter, in which it will default to System.Input a.k.a. standard input.

Thaddy

  • Hero Member
  • *****
  • Posts: 6349
Re: problem with textfile reading
« Reply #5 on: February 25, 2018, 08:24:47 am »
Just to correct: EOF doesn't need parameter, in which it will default to System.Input a.k.a. standard input.
EOF is indeed short for eof(Input) and not eof(yourfile); 
« Last Edit: February 25, 2018, 09:32:49 am by Thaddy »
I am enjoining wine, not whine....

Zoran

  • Hero Member
  • *****
  • Posts: 1243
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: problem with textfile reading
« Reply #6 on: February 25, 2018, 08:37:26 pm »
Let's make it simple...

See this line:
Code: Pascal  [Select]
  1. while not EOF do begin
Here EOF is end of which file?

EOF needs parameter.
Should be:
Code: Pascal  [Select]
  1. while not EOF(f) do begin
Just to correct: EOF doesn't need parameter, in which it will default to System.Input a.k.a. standard input.

But... it is obviously error in his code.
The fact that EOF without parameter defaults to standard input doesn't help, but makes the bug much harder to spot (because it compiles)...

Gebony

  • Newbie
  • Posts: 3
Re: problem with textfile reading
« Reply #7 on: March 02, 2018, 08:15:26 am »
thanks but the problem is the encoding of the file is unicode and i want to convert it to utf8 first.I tried a lot but the result is the same.

taazz

  • Hero Member
  • *****
  • Posts: 5149
Re: problem with textfile reading
« Reply #8 on: March 02, 2018, 01:02:06 pm »
thanks but the problem is the encoding of the file is unicode and i want to convert it to utf8 first.I tried a lot but the result is the same.
1) please specify what unicode is in your case
2) https://github.com/d-mozulyov/UniConv should provide all you need to convert your text.
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

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3399
  • I like bugs.
Re: problem with textfile reading
« Reply #9 on: March 03, 2018, 11:06:43 am »
thanks but the problem is the encoding of the file is unicode and i want to convert it to utf8 first.I tried a lot but the result is the same.
UTF-8 is one of the encodings of Unicode. Does your data use UTF-16 encoding? Then just use type "UnicodeString" and later assign to "String". No explicit conversion functions are needed.

Gebony

  • Newbie
  • Posts: 3
Re: problem with textfile reading
« Reply #10 on: April 15, 2018, 05:36:10 pm »
the encoding is "Little Endian"

jamie

  • Hero Member
  • *****
  • Posts: 773
Re: problem with textfile reading
« Reply #11 on: April 15, 2018, 06:10:07 pm »
Little endian is a byte order specific to a CPU platform...

 With intel based processures  it is Little endian... With others it could be big endian..


 All it means is the low part of a WORD, DWORD ect comes first in memory for little endian and
other way round for big endian..

 For Encodings, you have a single byte 0..127 value for ASCII and 128..255 for UTF8 code points or Extended ASCII.

 all in all, the ASCII and UTF8 are byte size characters and then you have WideString/Unicodestring which are WORD size
(2 bytes) characters..

 UTF8 confines itself in byte size characters but there are code points to where it takes multiple bytes for a specific char..

 So which one do you think you have ?


taazz

  • Hero Member
  • *****
  • Posts: 5149
Re: problem with textfile reading
« Reply #12 on: April 15, 2018, 06:13:20 pm »
the encoding is "Little Endian"
that is not a string encoding thats memory notation. see https://en.wikipedia.org/wiki/Endianness for the details.
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

engkin

  • Hero Member
  • *****
  • Posts: 2017
Re: problem with textfile reading
« Reply #13 on: April 15, 2018, 06:53:52 pm »
This is getting higher in spam index:
1-Code is posted in a screenshot. (February)
2-Wants to convert from Unicode to UTF8. (March)
3-The encoding is "Little Endian". (April)

Use LEtoN or Swap.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus