gpt4 book ai didi

c - 如何替换文件中的一行?

转载 作者:太空宇宙 更新时间:2023-11-04 08:29:39 26 4
gpt4 key购买 nike

我的程序只有当 main 函数很短时才工作,当它很长时它就停止工作。

我的程序卡在了这一行:

while (fgetc(f) != '\n');


void aktivpassziv(int sor, int aktiv, char rendszam[8], char helyzet)
{
int i = 0;
char tp;

FILE *f = fopen(".\\TAXI.txt", "r");
FILE *f2 = fopen(".\\temp.txt", "w");

while ((tp = fgetc(f)) != EOF)
{
if (tp == '\n') i++;

if (i == sor)
{
fprintf(f2, "\n%s\t%d\t%c\n", rendszam, !aktiv, helyzet);

while (fgetc(f) != '\n');
i++;
}
else
{
fprintf(f2, "%c", tp);
}


}
i = 0;

fclose(f);
fclose(f2);



}

我的文件包含车牌: union 行动ASD-123 0AABC-123 0乙HGK-187 1楼FDD-333 1K

最佳答案

代替

char tp;

使用

int tp;

如果 char 在您的平台中是无符号类型,tp 永远不会等于 EOF

然后换行

while (fgetc(f) != '\n')

while ( (tp = fgetc(f)) != '\n' && tp != EOF )

避免在到达文件末尾时陷入无限循环。

在您发布的代码中,您正在使用:

while (fgetc(f) != '\n');
i++;

不清楚您是打算使用那个还是打算使用:

while (fgetc(f) != '\n')
i++;

适本地使用我的建议。

关于c - 如何替换文件中的一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28994959/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com