//
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <stdio.h>
int fun1()
{
//添加Req和Ans
FILE *file = fopen("E:/test.aut","r+");
if (!file)
{
return -1;
}
char *strTollId = "83";
char *strVehicleId = "82";
int iToolIdIndex = 0;
int iVehicleIdIndex = 1;
int iCount = 0;
int iCountBeforeRead = 0;
while(!feof(file))
{
char buf[500] = {0};
fgets(buf,sizeof(buf) - 1,file);
//buf[strlen(buf) - 1] = '\n';//去掉\n
//buf[strlen(buf)] = '\n';//去掉\n
if (0 == strlen(buf))
{
fclose(file);
return -1;
}
else if (1 == strlen(buf))
{
iCount += 2;
continue;
}
else if (2 < strlen(buf))
{
iCount += strlen(buf) + 1;
}
char strFoundId[3] = {0};
memcpy(strFoundId,buf + iVehicleIdIndex*3,2);
if (!strcmp(strFoundId,strVehicleId))
{
char strReq[504] = {0};
strcpy(strReq,"Req:");
//先把插入位置后面的所有字符读取到临时文件中
FILE *tempFile = tmpfile();
if (!tempFile)
{
return -1;
}
while (!feof(file))
{
char bufTemp[500] = {0};
fgets(bufTemp,sizeof(bufTemp) - 1,file);
if(!feof(file))
{
// bufTemp[strlen(bufTemp) - 1] = '\r';//去掉\n
// bufTemp[strlen(bufTemp)] = '\n';
}
else
{
//最后一个字节读出来的是F F,下面的处理是去掉中间空格
// bufTemp[strlen(bufTemp) - 2] = bufTemp[strlen(bufTemp) - 1];
// bufTemp[strlen(bufTemp) - 1] = 0;
}
fwrite(bufTemp,1,strlen(bufTemp),tempFile);
}
fseek(file,iCount - (strlen(buf) + 1),SEEK_SET);
strcat(strReq,buf);
//strcat(strReq,"\\r\\n");
fwrite(strReq,1,strlen(strReq),file);
fflush(file);
iCount += 4;
fseek(tempFile,0,SEEK_SET);
while (!feof(tempFile))
{
char bufTemp[500] = {0};
fgets(bufTemp,sizeof(bufTemp) - 1,tempFile);
if(!feof(tempFile))
{
// bufTemp[strlen(bufTemp) - 1] = '\r';//去掉\n
// bufTemp[strlen(bufTemp)] = '\n';
}
else
{
//最后一个字节读出来的是F F,下面的处理是去掉中间空格
// bufTemp[strlen(bufTemp) - 2] = bufTemp[strlen(bufTemp) - 1];
// bufTemp[strlen(bufTemp) - 1] = 0;
}
fwrite(bufTemp,1,strlen(bufTemp),file);
}
fclose(tempFile);
fflush(file);
fseek(file,iCount,SEEK_SET);
}
else if (!strcmp(strFoundId,strTollId))
{
char strAns[504] = {0};
strcpy(strAns,"Ans:");
//先把插入位置后面的所有字符读取到临时文件中
FILE *tempFile = tmpfile();
if (!tempFile)
{
return -1;
}
while (!feof(file))
{
char bufTemp[500] = {0};
fgets(bufTemp,sizeof(bufTemp) - 1,file);
if(!feof(file))
{
// bufTemp[strlen(bufTemp) - 1] = '\r';//去掉\n
// bufTemp[strlen(bufTemp)] = '\n';
}
else
{
//最后一个字节读出来的是F F,下面的处理是去掉中间空格
// bufTemp[strlen(bufTemp) - 2] = bufTemp[strlen(bufTemp) - 1];
// bufTemp[strlen(bufTemp) - 1] = 0;
}
fwrite(bufTemp,1,strlen(bufTemp),tempFile);
}
fseek(file,iCount - (strlen(buf) + 1),SEEK_SET);
strcat(strAns,buf);
//strcat(strReq,"\\r\\n");
fwrite(strAns,1,strlen(strAns),file);
fflush(file);
iCount += 4;
fseek(tempFile,0,SEEK_SET);
while (!feof(tempFile))
{
char bufTemp[500] = {0};
fgets(bufTemp,sizeof(bufTemp) - 1,tempFile);
if(!feof(tempFile))
{
// bufTemp[strlen(bufTemp) - 1] = '\r';//去掉\n
// bufTemp[strlen(bufTemp)] = '\n';
}
else
{
//最后一个字节读出来的是F F,下面的处理是去掉中间空格
// bufTemp[strlen(bufTemp) - 2] = bufTemp[strlen(bufTemp) - 1];
// bufTemp[strlen(bufTemp) - 1] = 0;
}
fwrite(bufTemp,1,strlen(bufTemp),file);
}
fclose(tempFile);
fseek(file,iCount,SEEK_SET);
}
}
fclose(file);
return 0;
}
int fun2()
{
//添加Req和Ans
FILE *file = fopen("E:/test.aut","r+");
if (!file)
{
return -1;
}
int iCount = 0;
bool iIsFirstReq = true;
while(!feof(file))
{
char buf[500] = {0};
fgets(buf,sizeof(buf) - 1,file);
if (0 == strlen(buf))
{
fclose(file);
return -1;
}
else if (1 == strlen(buf))
{
iCount += 2;
continue;
}
else if (2 < strlen(buf))
{
iCount += strlen(buf) + 1;
}
char strReq[5] = {0};
memcpy(strReq,buf,4);
if (!strcmp(strReq,"Req:"))
{
if (iIsFirstReq)
{
iIsFirstReq = false;
continue;
}
//先把插入位置后面的所有字符读取到临时文件中
FILE *tempFile = tmpfile();
if (!tempFile)
{
return -1;
}
fwrite(buf,1,strlen(buf),tempFile);
while (!feof(file))
{
char bufTemp[500] = {0};
fgets(bufTemp,sizeof(bufTemp) - 1,file);
fwrite(bufTemp,1,strlen(bufTemp),tempFile);
}
fseek(file,iCount - (strlen(buf) + 1),SEEK_SET);
//strcat(strReq,buf);
//strcat(strReq,"\\r\\n");
fwrite("\n",1,1,file);
fflush(file);
iCount += 2;
fseek(tempFile,0,SEEK_SET);
while (!feof(tempFile))
{
char bufTemp[500] = {0};
fgets(bufTemp,sizeof(bufTemp) - 1,tempFile);
fwrite(bufTemp,1,strlen(bufTemp),file);
}
fclose(tempFile);
fseek(file,iCount,SEEK_SET);
}
}
fclose(file);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
//在文件指定位置插入字符串或者空行,下面2个函数是在每行前面加上Req或者Ans,并添加空行间隔
fun1();
fun2();return 0;
}
txt文件内容如下:
/*87 82 83 FF 54 FF
87 83 82 54 FF FF
87 82 83 48 FF FF
81 82 83 54 55 FF
85 83 82 79 FF FF
*/