Solved

Wrting to a file in C

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mixing C++ and C code elegantly 10 158
Adjust Mfcapp 29 172
distributed computing in Python 4 137
Need example 5 122
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

831 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