文本文件读写
- dword openFileRead (char filename[], dword mode);
- long fileGetBinaryBlock (byte buff[], long buffsize, dword fileHandle)
- long fileGetString (char buff[], long buffsize, dword fileHandle);
- dword openFileWrite (char filename[], dword mode);
- long fileWriteBinaryBlock (byte buff[], long buffsize, dword fileHandle);
- long filePutString (char buff[], long buffsize, dword fileHandle);
适用于 txt,csv ,xslx 等文本文件读写。
dword openFileRead (char filename[], dword mode);
- If mode=0 the file is opened in ASCII mode;
- if mode=1 the file is opened in binary mode.
https://www.cnblogs.com/cloud-ken/p/11671252.html
long fileGetBinaryBlock (byte buff[], long buffsize, dword fileHandle)
以二进制的方式打开文件,读取文件。
variables , on start ,on StopMeasurement 在下面的代码演示中不会再体现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | /*@!Encoding:1252*/ variables { dword glbHandle,glbHandle1 = 0; long mNewPage; // Sink identifier } on start { //Create a new page at the Write Window mNewPage= writeCreate("New Page"); //Configure the page so that its content is logged writeConfigure(mNewPage, 20, 1, "c:\\temp\\writelog.TXT"); //Clear content of CAPL page writeclear(1); writeLineEx(mNewPage,4,"\nKeyboard sequence: "); } On key 'w' { byte buffer_bin[10]; long retSize; int i; // Opens the file in binary mode for read access. glbHandle = OpenFileRead ("C:\\Data.txt",1); if ( glbHandle!=0 ) { do{ retSize = fileGetBinaryBlock(buffer_bin,elcount(buffer_bin),glbHandle); writeLineEx(mNewPage,1,"retSize:%d",retSize); for(i=0;i< retSize;i++) writeLineEx (mNewPage,1,"buffer_bin[%d]: %d.",i,buffer_bin[i]); }while(retSize > 0); fileClose (glbHandle); } on StopMeasurement { writeDestroy(mNewPage); fileClose (glbHandle); } |
Data.txt 的数据如下:
ASCii 对照表
输出:
Keyboard sequence:
retSize:10
buffer_bin[0]: 49.
buffer_bin[1]: 50.
buffer_bin[2]: 51.
buffer_bin[3]: 52.
buffer_bin[4]: 13.
buffer_bin[5]: 10.
buffer_bin[6]: 53.
buffer_bin[7]: 54.
buffer_bin[8]: 55.
buffer_bin[9]: 56.
retSize:10
buffer_bin[0]: 13.
buffer_bin[1]: 10.
buffer_bin[2]: 97.
buffer_bin[3]: 98.
buffer_bin[4]: 99.
buffer_bin[5]: 100.
buffer_bin[6]: 13.
buffer_bin[7]: 10.
buffer_bin[8]: 101.
buffer_bin[9]: 102.
retSize:2
buffer_bin[0]: 103.
buffer_bin[1]: 104.
retSize:0
注意点:
- buffer_bin[4]: 13.buffer_bin[5]: 10. 对应ASCII表中看出,会多打印出回车和换行
- retSize:10 ,retSize:10,retSize:2,retSize:0
因为定义的buffer_bin[10],读取最大的字符就是10个,retSize:2说明读取完毕了,且只读了两个字符,且如果读取完毕了,不会再打印13,10. - 接收读取字符 只能定义为byte 类型
- 读取文件的方式要选择 二进制读取
这里是引用
long fileGetString (char buff[], long buffsize, dword fileHandle);
变异函数为:long fileGetStringSZ(char buff[], long buffsize, dword fileHandle);
- fileGetString:会把行结束符(10),回车(13)也输入到接收buffer内
- fileGetStringSZ :不会把行结束符(10),回车(13)也输入到接收buffer内
- fileGetString 字符串格式输出打印
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | On key 'q' { char buffer_ascii[10]; long retSize; int i,j; glbHandle = OpenFileRead ("C:\\Data.txt",0); if ( glbHandle!=0 ) { j=0; while(fileGetString (buffer_ascii,elcount(buffer_ascii),glbHandle)!=0) { // %s 打印 writeLineEx (mNewPage,1,"lineNum:%d string: %s.",j,buffer_ascii); j++; // for(i=0;i< 6;i++) // writeLineEx (mNewPage,1,"buffer_bin[%d]: %d.",i,buffer_ascii [i]); } fileClose (glbHandle); } } |
输出: (注意每行输出之后都换行了,这是因为使用fileGetString 函数会把文件中的航结束符合换行符也算)
Keyboard sequence:
lineNum:0 string: 1234
.
lineNum:1 string: 5678
.
lineNum:2 string: abcd
.
lineNum:3 string: efgh.
- fileGetString 单个字符输出打印(为了凸显换行符)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | On key 'q' { char buffer_ascii[10]; long retSize; int i,j; glbHandle = OpenFileRead ("C:\\Data.txt",0); if ( glbHandle!=0 ) { j=0; while(fileGetString (buffer_ascii,elcount(buffer_ascii),glbHandle)!=0) { // writeLineEx (mNewPage,1,"lineNum:%d string: %s.",j,buffer_ascii); // j++; //单个字符%d打印 for(i=0;i< 6;i++) writeLineEx (mNewPage,1,"buffer_bin[%d]: %d.",i,buffer_ascii [i]); } fileClose (glbHandle); } } |
输出:
buffer_bin[0]: 49.
buffer_bin[1]: 50.
buffer_bin[2]: 51.
buffer_bin[3]: 52.
buffer_bin[4]: 10. // 换行,如果打开文件的方式是二进制打开,这里还会有回车符(13),参考上一个实例
buffer_bin[5]: 0.
buffer_bin[0]: 53.
buffer_bin[1]: 54.
buffer_bin[2]: 55.
buffer_bin[3]: 56.
buffer_bin[4]: 10.// 换行
buffer_bin[5]: 0.
buffer_bin[0]: 97.
buffer_bin[1]: 98.
buffer_bin[2]: 99.
buffer_bin[3]: 100.
buffer_bin[4]: 10.// 换行
buffer_bin[5]: 0.
buffer_bin[0]: 101.
buffer_bin[1]: 102.
buffer_bin[2]: 103.
buffer_bin[3]: 104.
buffer_bin[4]: 0.
buffer_bin[5]: 0.
- fileGetStringSZ字符串格式输出打印
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | On key 'q' { char buffer_ascii[10]; long retSize; int i,j; glbHandle = OpenFileRead ("C:\\Data.txt",0); if ( glbHandle!=0 ) { j=0; while(fileGetStringSZ (buffer_ascii,elcount(buffer_ascii),glbHandle)!=0) { writeLineEx (mNewPage,1,"lineNum:%d string: %s.",j,buffer_ascii); j++; // for(i=0;i< 6;i++) // writeLineEx (mNewPage,1,"buffer_bin[%d]: %d.",i,buffer_ascii [i]); } fileClose (glbHandle); } } |
输出:
lineNum:0 string: 1234.
lineNum:1 string: 5678.
lineNum:2 string: abcd.
lineNum:3 string: efgh.
- fileGetStringSZ 单个字符输出打印(为了凸显换行符)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | On key 'q' { char buffer_ascii[10]; long retSize; int i,j; glbHandle = OpenFileRead ("C:\\Data.txt",0); if ( glbHandle!=0 ) { j=0; while(fileGetStringSZ (buffer_ascii,elcount(buffer_ascii),glbHandle)!=0) { // writeLineEx (mNewPage,1,"lineNum:%d string: %s.",j,buffer_ascii); // j++; //单个字符%d打印 for(i=0;i< 6;i++) writeLineEx (mNewPage,1,"buffer_bin[%d]: %d.",i,buffer_ascii [i]); } fileClose (glbHandle); } } |
输出:(对比fileGetString 可以发现没了 换行字符(10))
buffer_bin[0]: 49.
buffer_bin[1]: 50.
buffer_bin[2]: 51.
buffer_bin[3]: 52.
buffer_bin[4]: 0.
buffer_bin[5]: 0.
buffer_bin[0]: 53.
buffer_bin[1]: 54.
buffer_bin[2]: 55.
buffer_bin[3]: 56.
buffer_bin[4]: 0.
buffer_bin[5]: 0.
buffer_bin[0]: 97.
buffer_bin[1]: 98.
buffer_bin[2]: 99.
buffer_bin[3]: 100.
buffer_bin[4]: 0.
buffer_bin[5]: 0.
buffer_bin[0]: 101.
buffer_bin[1]: 102.
buffer_bin[2]: 103.
buffer_bin[3]: 104.
buffer_bin[4]: 0.
buffer_bin[5]: 0.
dword openFileWrite (char filename[], dword mode);
打开文件写入
- 需要先设置打开文件的路径 setFilePath(),不可以直接setFilePath(“C:\”, 2); 因为对C根目录操作需要管理员权限。
long fileWriteBinaryBlock (byte buff[], long buffsize, dword fileHandle);
当文件以二进制的方式打开时,可以以二进制的方式写入数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | On key 'w' { byte buffer_bin[10]; byte buffer_write[10]={'y','o','u','s','e','l','f'}; long ret,retSize; int i,j=0; //写入 setFilePath("C:\\Temp", 2);//不可以直接设置C根目录,2表明可以对这个目录下的文件可读可写 glbHandle = openFileWrite ("Data.txt",1); //1 ,表明是二进制方式打开文件写 if ( glbHandle!=0 ) { retSize = fileWriteBinaryBlock (buffer_write, elcount(buffer_write), glbHandle); writeLineEx(mNewPage,1,"retSize:%d",retSize); fileClose (glbHandle); } //读取验证 glbHandle1 = OpenFileRead ("Data.txt",1); //1 ,表明是二进制方式打开文件,这里写入的路径必须是相对路径了,不可以是"C:\\Temp\\Data.txt",因为程序开始已经设置过操作的文件夹了 if ( glbHandle1!=0 ) { do{ retSize = fileGetBinaryBlock(buffer_bin,elcount(buffer_bin),glbHandle1); writeLineEx(mNewPage,1,"retSize:%d",retSize); for(i=0;i< retSize;i++) writeLineEx (mNewPage,1,"buffer_bin[%d]: %d.",i,buffer_bin[i]); }while(retSize > 0); fileClose (glbHandle1); } |
写入文件的结果
程序中读取验证的输出
retSize:10
retSize:10
buffer_bin[0]: 121.
buffer_bin[1]: 111.
buffer_bin[2]: 117.
buffer_bin[3]: 115.
buffer_bin[4]: 101.
buffer_bin[5]: 108.
buffer_bin[6]: 102.
buffer_bin[7]: 0.
buffer_bin[8]: 0.
buffer_bin[9]: 0.
retSize:0
long filePutString (char buff[], long buffsize, dword fileHandle);
向文件中写入字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | On key 'w' { char buffer_write[20]="yi xi wang yue"; long ret; int i,j=0; setFilePath("C:\\Temp", 2); glbHandle = openFileWrite ("Data.txt",0); //以ASCII打开,并且覆盖文件原有内容 if ( glbHandle!=0 ) { ret = filePutString (buffer_write, strlen(buffer_write), glbHandle); if(ret) { writeLineEx (mNewPage,1,"write ok."); fileClose (glbHandle); } else { writeLineEx (mNewPage,1,"write error."); } } } |
结果: