Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22248

How to edit a number which is at a specific location in a given file in C?

$
0
0

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);  
} 

Viewing all articles
Browse latest Browse all 22248

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>