Allen 2009-06-15 14:41:40 12618 16 0 0 0

Delphi读取文件和写入文件总结
---
读取文件:
1,关联文件:AssignFile(pMyFile,^c: tt.csv^);
2,打开文件:Reset(pMyFile);
3,读取一行:Readln(pMyFile,pStr);
4,关闭文件:CloseFile(pMyFile);

示例:

procedure TForm1.Button1Click(Sender: TObject);
var
  pMyFile:textfile;
  pStr : string;
begin

    if OpenDialog1.Execute then begin
        Assignfile(pMyFile,OpenDialog1.FileName);
        Reset(pMyFile);
        while not Eof(pMyFile) do begin
            Readln(pMyFile,pStr);
            //fn_DelStock(pStr);  //使用读取的字符串相关语句
            next;
        end;
        CloseFile(pMyFile);
    end;

end; 

+++
写入文件:
1,关联文件:AssignFile(pMyFile,^c: tt.csv^);
2,打开文件:ReWrite(pMyFile);   //如果文件不存在,用ReWrite打开
             Append(pMyFile);   //如果文件已经存在,追加内容,用Append打开
3,写入一行:WriteLn(pMyFile,pStr); 
4,关闭文件:CloseFile(pMyFile);

示例一:

procedure TForm1.Button1Click(Sender: TObject);

var

  i:integer  ;

  pMyFile: textfile;

begin

    system.AssignFile(pMyFile,^d: est.txt^);

    rewrite(pMyFile);

    {closefile(pMyFile);  

 

    system.AssignFile(pMyFile,^d: est.txt^);

    append(pMyFile);    // 此部分可有可无   }

    for i:=1 to 100 do begin

         writeln(pMyFile,intTostr(i)+^ X ^+intTostr(i)+^ = ^+intTostr(i*i));

    end ;

    closefile(pMyFile);

 

    showmessage(^写入文件完成!^);

end;

 

示例二:

procedure TForm1.WLog(pMsg: string);

var

  pFObJect,pFPath,pFName: string;

  pMyFile: textFile;

  pStr: string;

begin

 

    pFPath:=^d:^;

    pFName:=^StockDel_^+formatDateTime(^yyyymmddhhmmss^,now);

    pFObject:=pFPath + ^^ + pFName + ^.csv^;

    try

        AssignFile(pMyFile,pFObject);

        if FileExists(pFObject)=false then

            ReWrite(pMyFile)

        else

            Append(pMyFile);

        pStr:=pMsg;

        WriteLn(pMyFile,pStr);

    finally

        CloseFile(pMyFile);

    end;

 

end; 

 

+++
公用写日志文件过程

//==ini文件设置:
^日志选项和文件 当Log_Flag=N时不记录,否则均记录
  Log_Flag=1
  Log_PathFileName=\10.105.10.12cPrd_220_Filelog.dat

//==声明全局变量
  x_pLogFile: string;       //日志文件名称
  x_pLogFlag: string;       //是否记录日志,N:不写日志
  x_pFindLogFile: boolean;  //记录日志文件是否存在,避免每次写日志时都要判断。

//==过程声明
  procedure cpWriteLog(pFObject:string; pTxt:string; pMode:byte);

//==初始化全局变量
procedure TForm1.FormCreate(Sender: TObject);
begin
  x_pLogFile:= cfReadIniFile(X_cProgID,^Log_PathFileName^,^c:ENRC0300_Log.txt^);
  x_pLogFlag:= cfReadIniFile(X_cProgID,^Log_Flag^,^N^);
end;

//==写日志过程
procedure cpWriteLog(pFObject:string; pTxt:string; pMode:byte);
var
  pMyFile: textFile;
begin
    if x_pLogFlag=^N^ then exit;
    try
        AssignFile(pMyFile,pFObject);
        if pMode=1 then pTxt:= DateTimeToStr(Now)+^ ^+pTxt;
        if x_pFindLogFile=True then
            append(pMyFile)
        else begin
            if FileExists(pFObject) then
                append(pMyFile)
            else
                reWrite(pMyFile);
            x_pFindLogFile:=true;
        end;
        WriteLn(pMyFile,pTxt);
    finally
        CloseFile(pMyFile);
    end;
end;

//==调用过程
    x_pMsg:=^导出程序[^+X_cProgID+^]打开,^;
    cpWriteLog(x_pLogFile,x_pMsg,1);

   
 


【版權聲明】
本文爲原創,遵循CC 4.0 BY-SA版權協議!轉載時請附上原文鏈接及本聲明。
原文鏈接:https://tdlib.com/am.php?t=dSddohNBT8cJ
Tag: Delphi技巧 TTTBLOG
我也要發一個   ·   返回首頁   ·   返回[Delphi]   ·   前一個   ·   下一個
評論
Allen#1Allen 2009-12-29 09:35:19(N) 鏈接地址
| 787 | TTT | 2009-12-29 09:35:19 | | |
-----
调用ini文件时,要加引用单元inifiles.
Allen#2Allen 2014-06-22 12:14:10(N) 鏈接地址
| 2593 | FTVcsL | 2014-06-22 12:14:10 | [email protected] | http://www.wzzq.me/8612f2 |
-----
快乐赚,注册即送1元,参与活动赢大奖,免费抽50g千足金条、iPhone 5s 、iPad mini 、现金等,100%中奖。平均每天净赚60元,立即加入http://www.wzzq.me/8612f2Ou8gG
Allen#3Allen 2014-06-28 10:47:47(N) 鏈接地址
| 2621 | 香精香料加香机厂家批发 | 2014-06-28 10:47:47 | [email protected] | http://www.lxy158.com |
-----
香精香料 加香机总汇02061131338 18988992839 www.lxy158.comnuypy
Allen#4Allen 2014-06-29 19:25:13(N) 鏈接地址
| 2628 | 广州东森安防 | 2014-06-29 19:25:13 | [email protected] | http://www.yoooooy.com |
-----
嗯,确实挺不错的分享文章 www.yoooooy.com
Allen#5Allen 2014-07-03 17:16:56(N) 鏈接地址
| 2641 | daha | 2014-07-03 17:16:56 | [email protected] | |
-----
  智达上人水精念珠歌
Allen#6Allen 2014-09-29 09:08:20(N) 鏈接地址
| 2796 | 贴心商城 | 2014-09-29 09:08:20 | [email protected] | http://www.txgouwu.com |
-----
购物的天堂! 贴心商城,一个让消费者祈盼的季节;一个令市民们心动的时刻;一个给你与他省钱的机会;即日起,“贴心购物“您共同演绎一部真实而动人的换季大减价故事......贴心购物gInJX
Allen#7Allen 2014-09-29 14:08:25(N) 鏈接地址
| 2797 | FJyYVd | 2014-09-29 14:08:25 | [email protected] | http://www.yjcd.net |
-----
宇减传动机械有限公司设计生产各种搅拌器、机械搅拌器、反应釜搅拌器、化工搅拌器、沥青搅拌器、搅拌器配件、搅拌设备、搅拌装置、螺旋锥齿轮减速机、摆线针轮减速机、K、R系列减速机、皮带减速机、减速机机架、联轴器、机械密封、填料箱等产品。为您提供经济可靠的搅拌设备,解决最棘手的搅拌难题www.yjcd.net。WIl6I
Allen#8Allen 2014-10-01 11:23:52(N) 鏈接地址
| 2803 | 贴心商城 | 2014-10-01 11:23:52 | [email protected] | http://www.txgouwu.com |
-----
购物的天堂! 贴心商城,一个让消费者祈盼的季节;一个令市民们心动的时刻;一个给你与他省钱的机会;即日起,“贴心购物“您共同演绎一部真实而动人的换季大减价故事......贴心购物FoOuB
Allen#9Allen 2014-11-11 14:37:16(N) 鏈接地址
| 2929 | 锦峰电子回收 | 2014-11-11 14:37:16 | [email protected] | http://www.jfdzhs.com |
-----
这篇文章不错,欢迎看一下我回收电子料的网站www.jfdzhs.comzFKCc
Allen#10Allen 2014-11-12 11:40:45(N) 鏈接地址
| 2935 | F3OMQg | 2014-11-12 11:40:45 | [email protected] | |
-----
嗯,确实挺不错的分享文章PaI8y
頂部     1/2  ·  下頁
歡迎評論
未登錄,
請先 [ 註冊 ] or [ 登錄 ]
(一分鍾即可完成註冊!)
返回首頁     ·   返回[Delphi]   ·   返回頂部