#include
void modifyFile();
void modifyFile()
{
FILE *fp = NULL;
char key[19];
char curkey[16];
fpos_t pos;
long offset = 0;
fp = fopen("a.txt", "r+");
if (fp == NULL)
{
printf("fp is NULL\n");
return;
}
while ( fgets(key, 20, fp) != NULL )
{
printf("key is %s\n", key);
if (strlen(key) != 0)
{
if (key[strlen(key)-1] == '\n')
key[strlen(key)-1] = '\0';
strncpy(curkey, key, 16);
curkey[16] = 0;
printf("curkey is %s\n", curkey);
fgetpos(fp, &pos);
offset += strlen(key);
printf("fp position is %ld\n", pos);
if (strncmp(curkey, "2000000155FF1235", 16)==0)
{
fseek(fp, offset, SEEK_CUR);
fprintf(fp, "%s=%d\n", "2000000155FF1237", 4);
break;
}
}
}
fclose(fp);
}
void main()
{
FILE *fp = NULL;
fp = fopen("a.txt", "w");
fprintf(fp, "%s=%d\n", "2000000155FF1234", 1);
fprintf(fp, "%s=%d\n", "2000000155FF1235", 2);
fprintf(fp, "%s=%d\n", "2000000155FF1236", 3);
fclose(fp);
modifyFile();
}
阅读(1924) | 评论(0) | 转发(0) |