Forum > Beginners
problem with textfile reading
Gebony:
I have written codes for reading from file but the program can't read from it and have got empty screen.
please help me
Thaddy:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$ifdef fpc}{$mode delphi}{$H+}{$I-}{$endif}var path:string = 'profiles.txt'; line:string; f:Text; i:integer;begin Assign(f,path); reset(f); i:= IOResult; // check! https://www.freepascal.org/docs-html/current/user/userap4.html#x190-197000D if i <> 0 then writeln(i) else // or better writeln(STDERR, i) but that may confuse beginners while not eof(f) do begin readln(f, line); writeln(line); end; close(f);end.
Or:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$ifdef fpc}{$mode delphi}{$H+}{$I+}{$endif}uses sysutils;var path:string = 'profiles.txt'; line:string; f:Text;begin Assign(f,path); try reset(f); while not eof(f) do begin readln(f, line); writeln(line); end; close(f); except On E:Exception do writeln(E.Message); // or similar writeln(STDERR, E.Message); see above end;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.
RAW:
Here is an example with TFileStream as Thaddy already mentioned... works for me... :)
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Function LoadText(strFile: String; Out str: String): Boolean; Var fs : TFileStream; iBytes: Integer; Begin Try Result:= False; If FileExists(strFile) Then Begin fs:= TFileStream.Create(strFile, fmOpenRead); Try SetLength(str, fs.Size); iBytes:= fs.Read(str[1], fs.Size); If iBytes = fs.Size Then Result:= True; Finally fs.Free; End; End Else str:= 'FileExists Failed'; Except On E: Exception Do str:= 'LoadText Failed'+sLineBreak+E.ClassName+sLineBreak+E.Message; End; End;
PRG
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---PROGRAM LoadText;{$MODE OBJFPC}{$H+} USES Crt, Classes, SysUtils, Keyboard; VAR booQuit: Boolean; chKey : Char; strFileName, strLoad: String; Function KeyInput: Char; Var Key: TKeyEvent; Begin InitKeyboard; Try Key := GetKeyEvent; Key := TranslateKeyEvent(Key); Result:= GetKeyEventChar(Key); Finally DoneKeyboard; End; End; Function LoadText(strFile: String; Out str: String): Boolean; Var fs : TFileStream; iBytes: Integer; Begin Try Result:= False; If FileExists(strFile) Then Begin fs:= TFileStream.Create(strFile, fmOpenRead); Try SetLength(str, fs.Size); iBytes:= fs.Read(str[1], fs.Size); If iBytes = fs.Size Then Result:= True; Finally fs.Free; End; End Else str:= 'FileExists Failed'; Except On E: Exception Do str:= 'LoadText Failed'+sLineBreak+E.ClassName+sLineBreak+E.Message; End; End; BEGIN While Not booQuit Do Begin ClrScr; WriteLn(' LOAD TEXT FILE'); WriteLn('=================='); WriteLn('Enter A FileName:'); ReadLn(strFileName); If strFileName <> '' Then If LoadText(strFileName, strLoad) Then WriteLn(sLineBreak+'SUCCESS !!!'+sLineBreak+ '====='+sLineBreak+strLoad+sLineBreak+'=====') Else WriteLn(sLineBreak+'FAILURE !!!'+sLineBreak+strLoad); WriteLn; WriteLn; WriteLn('EXIT : ESC'); WriteLn('AGAIN: ENTER'); Repeat chKey:= KeyInput; Until (chKey = #27) Or (chKey = #13); If chKey = #27 Then Break; End;END.
Zoran:
Let's make it simple...
See this line:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---while not EOF do beginHere EOF is end of which file?
EOF needs parameter.
Should be:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---while not EOF(f) do begin
Leledumbo:
--- Quote from: Zoran on February 24, 2018, 09:11:24 pm ---Let's make it simple...
See this line:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---while not EOF do beginHere EOF is end of which file?
EOF needs parameter.
Should be:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---while not EOF(f) do begin
--- End quote ---
Just to correct: EOF doesn't need parameter, in which it will default to System.Input a.k.a. standard input.
Navigation
[0] Message Index
[#] Next page