Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Wrting to a file in C

Posted on 2002-04-09
5
Medium Priority
?
291 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:Dewaana
  • 2
  • 2
5 Comments
 
LVL 7

Expert Comment

by:leflon
ID: 6928455
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
0
 
LVL 7

Accepted Solution

by:
leflon earned 120 total points
ID: 6928462
better:
if((file=fopen("c:\mytext.txt","wt"))!=NULL)

leflon
0
 

Author Comment

by:Dewaana
ID: 6928558
Thank you very much, i think that if statment did the trick :)

Dewaana
0
 
LVL 1

Expert Comment

by:Aenox
ID: 9209314
lol, that wasn't the problem. This was:

FILE *file;
0
 
LVL 1

Expert Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question