Link to home
Start Free TrialLog in
Avatar of Dewaana
Dewaana

asked on

Wrting to a file in C

Hi
here is the code that i am trying to write or how i am trying to write into file

FILE *file
out = fopen("C:\myText.txt", "W");
fprintf(out, "Temprature");
fprintf(out, "%d\n", Voltage);
fclose(file);

but i can't get it to work, could somebody please tell me what i am doing wrong, and fix the problem for me. thanks

regards
Dewaana
Avatar of leflon
leflon
Flag of Germany image

first you mixed variable names file and out
then you better check if fopen was successful, and you should use wt for the access type

->
FILE* file;
if(file=fopen("c:\mytext.txt","wt")!=NULL)
{
/* do some output */
 fclose(file);
}

this should work

leflon
ASKER CERTIFIED SOLUTION
Avatar of leflon
leflon
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dewaana
Dewaana

ASKER

Thank you very much, i think that if statment did the trick :)

Dewaana
lol, that wasn't the problem. This was:

FILE *file;
FILE *out;
out = fopen("C:\\myText.txt", "W");
fprintf(out, "Temprature");
fprintf(out, "%d\n", Voltage);
fclose(out);