Delphi各种从文件里读取内容的方法
2016-10-14 21:10
465 查看
Hi I am having a problem running a function to read a text file the problem seems to be that my antivirus blocks my delphi console program because when I do for a visual form there is no problem . Tengos two codes one is this : function LeerArchivox(const filename: TFileName): String; var List: TStringList; begin if (FileExists(filename)) then begin List := TStringList.Create; List.Loadfromfile(filename); Result := List.text; List.Free; end; end; This goes to perfection but do not want to use the component Classes for the program does not weigh much . Also I have this : function leerarchivo(filealeer: string): string; var abriendo: TextFile; lineasleyendo: string; finaldearchivo: string; begin finaldearchivo := ''; AssignFile(abriendo, filealeer); Reset(abriendo); while not Eof(abriendo) do begin ReadLn(abriendo, lineasleyendo); finaldearchivo := finaldearchivo + lineasleyendo; end; CloseFile(abriendo); Result := finaldearchivo; end; Other code. function leerarchivo3(archivoaleer: string): string; const BUFF_SIZE = $8000; var dwread: LongWord; hFile: THandle; datafile: array [0 .. BUFF_SIZE - 1] of ansichar; codigofinal: string; begin codigofinal := ''; hFile := CreateFile(PChar(archivoaleer), GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, 0); SetFilePointer(hFile, 0, nil, FILE_BEGIN); Readfile(hFile, datafile, BUFF_SIZE, dwread, nil); while (dwread > 0) do begin Readfile(hFile, datafile, BUFF_SIZE, dwread, nil); codigofinal := codigofinal + datafile; end; Result := codigofinal; end; This is the problem because when I use my antivirus deletes it at the time , my question to other alternatives I have to read a text file without using Classes. Someone can help me? delphi
| |||||||||||||||||||||
|
4 Answers
activeoldestvotesup vote4down voteaccepted | You can use win32 api. In one of my apps I do things like that, extend/modify to match your needs. This only use Win32 API and does not lock the file. It's like notepad. When you open a file with notepad it is not locked and can still be written or read by other software. const BUFF_SIZE = $8000; var dwread:LongWord; hFile: THandle; datafile : array [0..BUFF_SIZE-1] of ansichar; //create file handler hFile := createfile(PChar(TFilePanel(FilePanelList.Items[i-1]).LongFileName), GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, 0); //set file pointer to beginning of file SetFilePointer(hFile, 0, nil, FILE_BEGIN); //read the file try Readfile(hFile, datafile, BUFF_SIZE, dwread, nil); while (dwread > 0) do begin //read/use datafile Here_do_something_with_datafile; Readfile(hFile, datafile, BUFF_SIZE, dwread, nil); end; finally closehandle(hFile); end;
| ||||||||||||||||||||
|
up vote1down vote | var txt : TextFile; begin AssignFile(txt,'filetxt.txt'); ReWrite(filetxt); Writeln(filetxt,memo1.Lines.Text); CloseFile(filetxt); Reset(filetxt); while not Eof(filetxt) do begin Readln(filetxt); end; CloseFile(filetxt);
| |||
add a comment |
up vote5down vote | This code works fine for me as a console application, Delphi 2007, running on Win7 64: Contents of 'E:\TempFiles\Test.txt': One Two Three Four Source: program Project2; {$APPTYPE CONSOLE} uses SysUtils; var Txt: TextFile; s: string; AllText: string; begin AllText := ''; AssignFile(Txt, 'E:\TempFiles\test.txt'); Reset(Txt); while not Eof(Txt) do begin Readln(Txt, s); AllText := AllText + s; // Write out each line; comment out to stop. Writeln(s); end; CloseFile(Txt); // Write out all content as a single string. WriteLn(AllText); ReadLn; end. Produces output: One Two Three Four
| ||||||||||||||||||||
|
up vote0down vote | If you don't want to use the Classes unit, only need to operate on this file, and are making a Windows executable, you could use the Windows unit instead and call the Win32 API functions: CreateFile, ReadFile, CloseHandle and related functions.
|
相关文章推荐
- 读取TXT文件内容的方法
- 读取TXT文件内容的方法
- 读取XML文件内容的简单方法
- jsp中读取xml文件中内容的方法
- 读取TXT文件内容的方法
- sql 读取txt 文件内容,并写入sql的方法
- 用c#读取文件内容中文是乱码的解决方法
- 读取文件内容到int数组的函数处理方法
- API读取写入 ini文件内容的方法函数详解 【转】
- java读文本文件内容---传入path来读取方法
- sql 读取txt 文件内容,并写入sql的方法
- 读取web.xml中各种类型参数的方法(读取资源文件)
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- 用c#读取文件内容中文是乱码的解决方法
- API读取写入 ini文件内容的方法函数详解 【转】
- java各种读取文件的方法
- java 读取properties配置文件内容乱码 --日文乱码对应方法
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- 【Matlab】 读取文件各种方法
- delphi 快速读取文件 然后用TPerlRegEx类提取需要内容