I have saved a list of number as
1
2
3
4
5
6
7
8
9
10
in the file INPUT.txt
. and I want to edit a specific location (which is the 5th element here) and want to replace it by number 35
. How can I do this? (I do not want to create another new file, neither I want to overwrite the whole thing, just editing that file only!!).
#include <stdio.h>
void main() {
FILE *fp;
char ch;
int a, i, b = 35;
fp = fopen("INPUT.txt", "r+");
for (i = 0; i < 10; i++) {
fscanf(fp, "%d", &a);
printf("\t%d\n", a);
if (i == 5) {
fprintf(fp, "b");
}
}
fclose(fp);
}