发布于 2008-05-10 20:47:49
0楼
用c s cript有两种方法,一种简单的就是使用ansi c函数,代码如下:
FILE* lpFile;
char* lpszStr = "hello\r\n";
lpFile = fopen("c:\\test.txt", "a");
if(lpFile == NULL)
{ printf("can not open file\r\n"); return;}
fprintf(lpFile, lpszStr);
fclose(lpFile);
但是WinCC中使用printf,fprintf等函数最多只能处理360个字符,如果你写的字符串过长,可以使用windows api,代码如下:
#pragma code("kernel32.dll")
#include "windows.h" // 这里的window.h必须要装上windows sdk
// 或者装个vc6.0
// 然后把vc98\include里的头文件拷贝到
// siemens\wincc\aplib中
#pragma code()
HANDLE hFile;
DWORD dwPos, dwBytes;
char* lpszText = "hello\r\n";
int nLen = strlen(lpszText);
hFile = CreateFile("c:\\test.txt", GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile == INVALID_HANDLE_VALUE)
{printf("can not open file\r\n"); return;}
dwPos = SetFilePointer(hFile, 0, NULL, FILE_END);
LockFile(hFile, dwPos, 0, dwPos + nLen, 0);
WriteFile(hFile, lpszText, nLen, &dwBytes, NULL);
UnLockFile(hFile, dwPos, 0, dwPos + nLen, 0);
CloseHandle(hFile);
ps:如果编译时出现常量没有定义的错误,自行去winbase.h中找到定义并在脚本中自己预定义一下。