gpt4 book ai didi

c - 使用 fseek 编辑文件

转载 作者:太空宇宙 更新时间:2023-11-04 06:09:06 25 4
gpt4 key购买 nike

你能帮我弄清楚我的代码中有什么问题吗..我想编辑特定的行....谢谢

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main (){
char arr[50];
char arr2[50];
char arr3[50];
FILE *stream = NULL;
FILE *stream2 = NULL;
stream = fopen("studentinfo.txt", "rt");
stream2 = fopen("studentinfo2.txt", "w+");
char* token;
char dlm[] = ",";

printf("Enter student id: ");
scanf("%s", arr2);
printf("New student id: ");
scanf("%s", arr3);
while(!feof(stream)){
fgets(arr,100,stream);
fprintf(stream2,"%s",arr);
token = strtok(arr,dlm);
if(strcmp(arr2, token)==0){
fseek ( stream2 , 0 , SEEK_CUR );
fputs ( arr3 , stream2 );
}
}
fclose ( stream2 );
fclose ( stream );
getch();
}

最佳答案

您不能“编辑特定行”。您可以替换文件中当前的字节,但是如果您需要替换为更少或更多的字节,那么您需要在它们之后重写文件的其余部分。

关于c - 使用 fseek 编辑文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4358598/

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