• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Delphi各种从文件里读取内容的方法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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?

improve this question
 
3  
I don't know about what your AV software is doing, but the second code you posted isn't real, because it won't compile. (finalarchivo : = + lineasleyendo finaldearchivo is not valid Delphi syntax. If you want help with your code, post your real code. We can't debug code you make up as you go, because you could change the real problem while you're making it up. – Ken White Nov 14 '13 at 16:41
    
You can use the solution with TStringList also from a console program! Classes and components / visual forms are two very different things. – jpfollenius Nov 14 '13 at 16:44
    
ken, the real code is that google traslate is that I moved the code.jpfollenius, what I'm looking for is how to do it without TStringList – Jose Martinez Nov 14 '13 at 16:48 
1  
@Jose: Then don't run your code through Google Translate. Run your text, but not the code. – Ken WhiteNov 14 '13 at 17:03
    
"This goes to perfection but do not want to use the component Classes for the program does not weigh much ." -> So you weight the weight of the resulting binary higher than a good design? Then I recommend strongly to turn off $R,D,L,Q and all other compiler/linker settings regarding assertions, checks and debug symbols. Will make debugging worse, but the size of the EXE shrinks dramatically. – JensG Nov 14 '13 at 17:52 
add a comment

4 Answers

up 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;
improve this answer
 
    
thanks for the help hpterm, the code is perfect, update your code in my post, because I'm making a function called leerarchivo3 () in your code, the problem is not how to use datafile variable to store the total content of the variable codigofinal, I ask you to tell me I did or wrong or I can do what I want, I hope not ask for much – Jose Martinez Nov 14 '13 at 17:22
    
I thought your question was how to open a file without using TStringList. As @StijnSanders said, in that case use Win32 API. That's what I show you here. If now the question has changed, ask another one. I'm sorry I am not sure I understand what you need now. – HpTerm Nov 14 '13 at 17:25
    
the problem I have now is that when using your code try to keep the entire contents of that file in your code is datafile in a single string called codigofinal what I mean to return after using codigofinal value Result:=codigofinal – Jose Martinez Nov 14 '13 at 17:40
1  
@JoseMartinez: My code shows how to do that using AssignFile, which is what you originally asked. If mine doesn't answer, and HpTerm's doesn't answer, and Stijn's doesn't answer, then your question is very unclear, and you need to edit to improve it so we know what you're really asking. – Ken White Nov 14 '13 at 17:43 
1  
@Jose: My code does in fact work; I even showed the output. – Ken White Nov 14 '13 at 17:51
show 1 more comment
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);
improve this answer
 
add a comment

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
OneTwoThreeFour
improve this answer
 
    
+1 for a working answer. Seems the OP is not very clear about what he wants. – HpTerm Nov 14 '13 at 17:49
    
@HpTerm: Thanks. I've done the same for yours. Seems he's finally decided, too; you got the accept as well. – Ken White Nov 14 '13 at 17:50
2  
Next time I see someone use old-style file functions ... don't know what to do. We really need a banned API list in Delphi. – JensG Nov 14 '13 at 17:55
    
@JensG: Agreed. I haven't used them since TFileStream was introduced. I had to do some dusting to uncover the memories of how to do so. :-) – Ken White Nov 14 '13 at 18:00
    
@JensG : I also agree. – HpTerm Nov 14 '13 at 18:23
show 1 more comment

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: CreateFileReadFileCloseHandle and related functions.

improve this answer

 

https://stackoverflow.com/questions/19983202/read-text-files-in-delphi


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
matlab如何将一个矩阵的任意两行或两列交换发布时间:2022-07-18
下一篇:
LaTeX文章中插入Visio及Matlab矢量图(转载)发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap