Adding data to existing data in an input file?
Hello,
I am working on a program (other than the attached example).
At first my FILE was declared in "r" (read) mode, then I realized that in order to write i need it to be in "w" (write) mode, but that ONLY for if I am NOT reading and writing to the same file at the same time.
So finally, I changed it to "a" (append), which is suppose to allow me to add to the data to the existing data in the input file. But it is not working, can someone please help me?
Below, is a oversimplified example of what I am trying to do, the input file contains some data such as integers for example 5 and 4. I want to read these integers from the input file and write the sum of 5+4 into the input file that I just read from so the input file that just contained "5 4" will now contain "5 4 9".
Code:
#include <stdio.h>
int main (void){
int num1, num2, sum;
FILE *ifp;
ifp = fopen("input.txt", "a");
fscanf(ifp, "%d", num1);
fscanf(ifp, "%d", num2);
sum=num1+num2;
fprintf(ifp, "%d", sum);
fclose(ifp);
return 0;
}
Thanks in advance.