I’m trying to write to a file in C, so quite simple stuff. But I am encountering an issue with the below code:
#include<stdio.h>
#include<stdlib.h>
int main(){
FILE *fptr;
int num;
fptr = fopen("C:\test.txt","w");
if(fptr == NULL){
printf("Error!");
exit(1);
}
printf("Enter your number");
scanf("%d",&num);
printf("Your number is: %d",&num);
fprintf(fptr,"%d",num);
fclose(fptr);
return 0;
}
It never prints the value of num, and the text file always just ends up with an incorrect value.
I’ve tried over and over again to fix the problem, changing the values I put into it, trying to fiddle around the with the MGM compiler I am using to see if it’s a compiler issue, to no avail. I’ve even tried copying other C code that writes to text files, only to get the same issue.
It always just writes 0.
Owl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.