所以我遇到了使用 Visual C++ 编辑文件处理的问题。我现在拥有的代码将显示文件内部的内容,在编辑它时,你必须重新输入文件的所有包含内容,包括你编辑的内容。但是我的教授说它不是编辑而是覆盖原始文件..所以我想知道如何在不重写整个语句的情况下编辑文件的包含。
void edit()
{
char choice;
char newdata[1000];
char previousdata[1000];
char filename [1000];
int count =0;
printf("Example: D:/sample.txt");
printf("Enter filename:");
scanf("%s",&filename);
fp=fopen(filename,"r");
if(fp==NULL)
{
printf("Unable to open....\n");
}
else
{
printf("Success in opening file...\n\n");
int c;
c = getc(fp);
while(c!=EOF)
{
printf("%c",c);
previousdata[count] = c;
c = getc(fp);
count++;
}
}
fclose(fp);
printf("\nPlease Type the new statement");
printf("\nInput Statement:\n");
scanf("%s",&newdata);
printf("\nDo you want to save the changes?");
printf("\n Press[Y] YES \t\t Press[N] NO");
printf("\n Your choice:");
scanf("%s",&choice);
if(choice == 'Y'|| choice == 'y')
{
fp =fopen(filename,"w");
fprintf(fp,"%s",newdata);
fclose(fp);
main();
}
else if(choice == 'N'||choice == 'n')
{
system("CLS");
main();
}
}