一、某些特定场景 ,如需要处理一些简单TXT文本文件,如串口数据时,则简单生成exe文件可以方便处理。
简单的c文件参考如下,主要包含文件文件的打开、读取和保存等等。
#include <stdio.h>
#include <string.h>
#include <dirent.h>
FILE *fsmoke;
FILE *fppm;
void CreatFile(char *file_name)
{
}
int getTempValue(char *str)
{
char slipe[] = ":";
char *p = str;
p = strtok(p, slipe);
p = strtok(NULL, slipe);
return atoi(p);
}
void getSmokeData(char *str, char *tempstr)
{
char slipe[] = ":,";
char *p = str;
p = strtok(p, slipe);
p = strtok(NULL, slipe);
fputs("T=", fsmoke);
fputs(tempstr, fsmoke);
fputs(", ", fsmoke);
fputs(p, fsmoke);
fputs("\n", fsmoke);
// printf("%s\n", p);
}
void getPpmData(char *str, char *tempstr)
{
char slipe[] = ",";
char *p = str;
p = strtok(p, slipe);
for (int i = 0; i < 4; i++)
p = strtok(NULL, slipe);
// printf("%s", p + 7);
fputs("T=", fppm);
fputs(tempstr, fppm);
fputs(", ", fppm);
fputs(p + 7, fppm);
}
int main(int argc, char *arg)
{
char file_name[256] = "./a.txt";
FILE *fp;
DIR *pdir;
struct dirent *pdirent;
char StrLine[1024];
char smoke_string[128];
char ppm_string[128];
char slipe[] = ":,";
int TempValue = 0;
int TempValue2 = 0;
char TempStr[128];
int Index = 0;
if ((pdir = opendir("./")) != NULL)
{
while (pdirent = readdir(pdir))
{
//����"."��".."
if (strcmp(pdirent->d_name, ".") == 0 || strcmp(pdirent->d_name, "..") == 0)
continue;
if (strstr(pdirent->d_name, ".txt") != NULL)
{
strcpy(file_name, pdirent->d_name);
}
}
}
else
{
goto error;
}
if ((fp = fopen(file_name, "r")) == NULL)
{
printf("no file open\n");
goto error;
}
if ((fsmoke = fopen("smoke.txt", "wt")) == NULL)
{
printf("smoke fail\n");
goto error;
}
if ((fppm = fopen("ppm.txt", "wt")) == NULL)
{
printf("ppm fail\n");
goto error;
}
while (!feof(fp))
{
for(TempValue=-15; TempValue<69; )
{
//printf("T=%s\n", itoa(TempValue));
//printf("T1=%d\n", TempValue);
fgets(StrLine, sizeof(StrLine), fp);
if (strncmp(StrLine, "T:", 2) == 0)
{
//printf("L=%s", StrLine);
//printf("T1=%d\n", TempValue);
//printf("T2 ok = %d\n\n", getTempValue(StrLine));
TempValue2 = getTempValue(StrLine);
if((TempValue2 == TempValue) && (++Index == 5))
{
//printf("T3=%d\n", TempValue);
Index = 0;
fgets(StrLine, sizeof(StrLine), fp);
fgets(StrLine, sizeof(StrLine), fp);
if (strncmp(StrLine, "Smoke:", 6) == 0)
{
printf("Smoke: %s, %d, %d\n", StrLine, TempValue2, TempValue);
itoa(TempValue, TempStr, 10);
getSmokeData(StrLine, TempStr);
TempValue++;
}
}
}
if(feof(fp))
{
printf("file end\n");
break;
}
}
}
if ((fp = fopen(file_name, "r")) == NULL)
{
printf("no file open\n");
goto error;
}
while (!feof(fp))
{
for(TempValue=-15; TempValue<69; )
{
fgets(StrLine, sizeof(StrLine), fp);
if (strncmp(StrLine, "T:", 2) == 0)
{
TempValue2 = getTempValue(StrLine);
if((TempValue2 == TempValue) && (++Index == 3))
{
Index = 0;
fgets(StrLine, sizeof(StrLine), fp);
fgets(StrLine, sizeof(StrLine), fp);
fgets(StrLine, sizeof(StrLine), fp);
fgets(StrLine, sizeof(StrLine), fp);
fgets(StrLine, sizeof(StrLine), fp);
fgets(StrLine, sizeof(StrLine), fp);
fgets(StrLine, sizeof(StrLine), fp);
fgets(StrLine, sizeof(StrLine), fp);
fgets(StrLine, sizeof(StrLine), fp);
fgets(StrLine, sizeof(StrLine), fp);
fgets(StrLine, sizeof(StrLine), fp);
//fgets(StrLine, sizeof(StrLine), fp);
if (strncmp(StrLine, "T0", 2) == 0)
{
printf("Co: %s, %d, %d\n", StrLine, TempValue2, TempValue);
itoa(TempValue, TempStr, 10);
getPpmData(StrLine, TempStr);
TempValue++;
}
}
}
if(feof(fp))
{
printf("file end\n");
break;
}
}
}
#if 0
for(TempValue=-15; TempValue++; TempValue<69)
{
if (strncmp(StrLine, "T:", 2) == 0)
{
if(getTempValue(StrLine) == TempValue)
{
if (strncmp(StrLine, "T0", 2) == 0)
{
getPpmData(StrLine);
}
}
}
}
#endif
fclose(fp);
fclose(fsmoke);
fclose(fppm);
printf("ok\n");
getchar();
return 0;
error:
fclose(fp);
fclose(fsmoke);
fclose(fppm);
getchar();
return -1;
}
二、需要安装GCC,命令窗口下gcc -v可以查看
生成exe命令:gcc read.c -o test,即编译read.c生成test.exe.
版权声明:本文为xqw19891201原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。