C语言文件按行修改

void file_update_test()
{
	FILE* fp;
	char buf[1024] = { 0 };
	fp = fopen("1.txt","rb+");
	int update_index = 2;
	int cnt = 0;
	if (fp == NULL)
	{
		printf("open fail");
		return;
	}

	while (fgets(buf, sizeof(buf), fp))
	{
		cnt++;
		printf("buf=%s\n", buf);
		if (update_index == cnt)
		{
			fseek(fp, -strlen(buf), SEEK_CUR); // 把文件指针移动回当前行的开头
			fprintf(fp, "202020");
			break;
		}
	}
	fclose(fp);
}
posted @ 2023-04-16 10:08  萧海~  阅读(52)  评论(0编辑  收藏  举报