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?

asked Nov 14 '13 at 16:36
Jose Martinez

1281312
 
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 

4 Answers

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;
answered Nov 14 '13 at 17:06
HpTerm

5,850124062
 
    
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
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);
answered Sep 15 '14 at 8:50
 

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
answered Nov 14 '13 at 17:20
Ken White

98.6k11118213
 
    
+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

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.

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

Delphi各种从文件里读取内容的方法的更多相关文章

  1. delphi xe4 ini文件不能读取的解决方法

    今天发现用inifiles下 tinifile.readstring方法突然不能读数据了,结果把ini文件格式由utf-8改成unicode后就能正常读取了.

  2. Flex读取txt文件里的内容(二)

    Flex读取txt文件里的内容 自己主动生成的文件 LoadTxt-app.xml: <?xml version="1.0" encoding="utf-8&quo ...

  3. Flex读取txt文件里的内容(一)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/you23hai45/article/details/25248307  Flex读取txt文件里的内 ...

  4. Flex读取txt文件里的内容报错

    Flex读取txt文件里的内容 1.详细错误例如以下 2.错误原因 读取文件不存在 var file:File = new File(File.applicationDirectory.nativeP ...

  5. 从Excel文件中读取内容

    从Excel文件中读取内容 global::System.Web.HttpPostedFileBase file = Request.Files["txtFile"]; strin ...

  6. element ui 上传文件,读取内容乱码解决

    element ui 上传文件,读取内容乱码解决: 加第二个参数 reader.readAsText(file.raw,'gb2312'); <el-upload class="upl ...

  7. web打开本地文件并读取内容

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. php mysqli query 查询数据库后读取内容的方法

    php mysqli query 查询数据库后读取内容的方法 <?php$mysqli = new mysqli("localhost", "my_user&quo ...

  9. Jupyter notebook导入Pycharm项目的.py文件里的模块及方法

    Jupyter notebook导入Pycharm项目种的.py文件里的模块及方法 需要在Jupyter notebook里调用自己写的代码,过程如下. 首先在Pycharm里写好一个文件,例如DCC ...

随机推荐

  1. C# powshell 调用

    原文:C# powshell 调用 本文告诉大家如何在 ps 脚本使用 C# 代码. 首先创建一个 C# 的控制台项目,注意修改输出为类库. 现在的 Powershell 还不支持 dotnet co ...

  2. gdal库集成MrSID库的做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 首先从Lizardtech网站:http://www.lizardtech.com/download/develope ...

  3. Asp.net C# 获取本周上周本月上月本年上年第一天最后一天时间大全

    DateTime dt = DateTime.Now; int weeknow = Convert.ToInt32(DateTime.Now.DayOfWeek); ) * weeknow + ; D ...

  4. Qt5.9 官方发布的新版本亮点的确不胜枚举(而且修复2000+ bugs)

    作者:Summer Fang链接:https://www.zhihu.com/question/60486611/answer/177584284来源:知乎著作权归作者所有.商业转载请联系作者获得授权 ...

  5. go 工具链目前[不支持编译 windows 下的动态链接库][1],不过[支持静态链接库][2]

    go 工具链目前[不支持编译 windows 下的动态链接库][1],不过[支持静态链接库][2].想要产生dll,可以这样 workaround ,参考 golang [issuse#11058][ ...

  6. 分享 WebBrowser显示Html内容3点细节技巧,解决刷新后空白

    直接显示Html内容,不像直接导航网址容易处理: 问题:按微软的控件属性提示,可以用WebBrowser.DocumentText 属性赋值 ,但是这种赋值,只是首次有效,后续切换都没啥作用. 下面给 ...

  7. ubuntu 12.04 简单配置samba服务,实现主机与虚拟机互通(设置Windows虚拟盘)

    环境: virtualbox ubuntu12.04 首先,如果你到这步了,说明你的window与linux的网络已经配好了,他们之间是可以互相Ping通的,如果没有,请看我以前的文章 由于我linu ...

  8. Android获取百度音乐下载音乐和歌词下载链接

    首先,你必须通过以下连接下载歌曲: http://box.zhangmen.baidu.com/x?op=12&count=1&title={title}$${author}$$$$ ...

  9. PYC文件简介

    PYC文件简介¶ 不说废话,这里说的pyc文件就是 Python 程序编译后得到的字节码文件 (py->pyc). 基本格式¶ pyc文件一般由3个部分组成: 最开始4个字节是一个Maigc i ...

  10. WPF中使用cefsharp

    原文:WPF中使用cefsharp 新入职一家公司,由写服务端接口变成了软硬件通信.服务器.客户端.C/S.B/S乱七八糟各种打杂.首先接收一个WPF项目,因为不熟WPF,再加上前端我也不熟,我打算使 ...