把数据写到外部文件中,然后再从外部文件中读取,(读写外部文件)参考资料:
http://support.automation.siemens.com/CN/view/zh/850338
资料中的读写都是以32为浮点型变量来实验,但是我真正想要的是保存和读取一些字符串。
下面这段都函数该怎样改:
#include "apdefap.h"
void OnClick(char* lpszPictureName, char* lpszObjectName, char* lpszPropertyName)
{
#define MaxLineLength 80
FILE *fpFile;
char *strTag, *strValue, *pTmp;
char buffer[MaxLineLength];
double dVal;
fpFile = fopen(GetTagChar("FileName"), "r" ); //open file to read
printf ("C-Script: read file: %s\r\n", GetTagChar("FileName"));
if (fpFile !=NULL)
{
while (fgets (buffer, MaxLineLength , fpFile))
{
strTag = buffer;
// Split String and cut carriage return
pTmp = strchr(buffer, (int)';');
*pTmp = '\0';
strValue = ++pTmp;
strValue[strlen(strValue)-1] = '\0';
// Search for ',' and replace with '.'
pTmp = strchr(strValue, (int)',');
if (pTmp != NULL){
*pTmp = '.';
}
// Convert Value-String to double and write to WinCC
sscanf (strValue , "%lf", &dVal);
SetTagDouble(strTag,dVal);
printf("%s Value: %lf\r\n", strTag, dVal);
}
fclose(fpFile); //close file
} else {
printf ("Error: File not found!\n");
}
}
最佳答案
double dVal; 改为:char dval
sscanf (strValue , "%lf", &dVal); 改为:
sscanf (strValue , "%20s", &dVal);
SetTagDouble(strTag,dVal);改为:SetTagchar(strTag,dVal);
输出也改一下.不知道是否可行.
提问者对于答案的评价:
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc267052.html