Solved

Wrting to a file in C

Posted on 2002-04-09
5
273 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 30 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

Independent Software Vendors: 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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

730 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