c语言 修改指定坐标内容,怎么修改txt文件中某个位置的数据

本文介绍了一个使用C/C++实现的程序,该程序能够从文本文件中读取特定格式的数据,并根据预设的规则修改部分字段值,最后将修改后的数据重新写回文件。涉及的数据结构包括字符数组和结构体,展示了文件操作、字符串处理和条件判断等基本编程技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C/C++ code#include

#include

#include

#define MAX_LEN 20

#define ARR_LEN 50

struct st_strdata

{

char thickness[MAX_LEN];

char u1[MAX_LEN];

char u2[MAX_LEN];

char a0[MAX_LEN];

char a1[MAX_LEN];

};

int main()

{

int nRes = 0;

int i = 0;

int iCount = 0;

struct st_strdata sdata[ARR_LEN] = {0};

FILE* fp = fopen("ma.txt", "r");

i = 0;

iCount = 0;

while(1)

{

nRes = fscanf(fp, "%s%s%s%s%s",

sdata[i].thickness,

&sdata[i].u1,

&sdata[i].u2,

&sdata[i].a0,

&sdata[i].a1);

++iCount;

++i;

if(-1 == nRes)

{

break;

}

}

fclose(fp);

printf("原数据: \n");

for(i = 0; i

{

printf("%s %s %s %s %s\n",

sdata[i].thickness,

sdata[i].u1,

sdata[i].u2,

sdata[i].a0,

sdata[i].a1);

}

printf("\n");

fp = fopen("ma.txt", "w");

for(i = 0; i

{

if(0 == strcmp(sdata[i].u1, "u1"))

{

fprintf(fp, "%s %s %s %s %s\n",

sdata[i].thickness,

sdata[i].u1,

sdata[i].u2,

sdata[i].a0,

sdata[i].a1);

continue;

}

if (0 == strcmp(sdata[i].thickness, "0.19-0.199"))

{

if (0 == strcmp(sdata[i].u1, "0.2"))

{

strcpy(sdata[i].u1, "0.19");

}

if (0 == strcmp(sdata[i].u2, "0.115"))

{

strcpy(sdata[i].u2, "0.11");

}

if (0 == strcmp(sdata[i].a0, "-5"))

{

strcpy(sdata[i].a0, "-4");

}

if (0 == strcmp(sdata[i].a1, "-2"))

{

strcpy(sdata[i].a1, "-1.8");

}

}

if (0 == strcmp(sdata[i].thickness, "0.20-0.209"))

{

if (0 == strcmp(sdata[i].u1, "0.196"))

{

strcpy(sdata[i].u1, "0.18");

}

if (0 == strcmp(sdata[i].u2, "0.1145"))

{

strcpy(sdata[i].u2, "0.10");

}

if (0 == strcmp(sdata[i].a0, "-5"))

{

strcpy(sdata[i].a0, "-4");

}

if (0 == strcmp(sdata[i].a1, "-2"))

{

strcpy(sdata[i].a1, "-1.8");

}

}

fprintf(fp, "%s %s %s %s %s\n",

sdata[i].thickness,

sdata[i].u1,

sdata[i].u2,

sdata[i].a0,

sdata[i].a1);

}

fclose(fp);

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值