C文件编译生产exe可执行文件

一、某些特定场景 ,如需要处理一些简单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.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值