I've reviewed the documentation:
It says here:
Once a file has been successfully opened, you can read from it using fscanf() or write to it using fprintf(). These functions work just like scanf() and printf(), except they require an extra first parameter, a FILE * for the file to be read/written.
So, I wrote my code as such, and I made sure to include a conditional statement to make sure that the file opened:
# include<stdio.h>
# include<stdlib.h>
void from_user(int*b){
b = malloc(10);
printf("please give me an integer");
scanf("%d",&b);
}
void main(){
FILE *fp;
int*ch = NULL;
from_user(ch);
fp = fopen("bfile.txt","w");
if (fp == NULL){
printf("the file did not open");
}
else {
printf("this is what you entered %d",*ch);
fprintf(fp,"%d",*ch);
fclose(fp);
free(ch);
}
}
Am I wrong or is the documentation not explaining this correctly? thanks.