Solved

I need to know how to print to external files in C language!! Easy question for pros.

Posted on 2003-11-19
7
258 Views
Last Modified: 2010-04-15
Hey techies:

I need to know how to print to an external file.
how would I have this command:

 printf("This is the car %s", car);

actually print to an external file, like a .txt

Please help, I need to know this ASAP. Tell me what function and what header file to include, also what library is it under.

thanks
Junior161
0
Comment
Question by:Junior161
7 Comments
 
LVL 5

Expert Comment

by:migoEX
ID: 9785240
#include <stdio.h>

FILE *stream = fopen("my.txt", "w");
fprintf( stream, "printing text to file\n" );
fclose( stream );
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9785247
Use can use FILE related functions

Declare a FILE *fp pointer
Open the file u want to write to in write mode using fopen ()
write to the file using fwrite() OR fprintf()
close the file using fclose()

HTH

amit



0
 

Author Comment

by:Junior161
ID: 9785279
Yeah, but that wont allow me to print other variables. For example if I were to print

int car =0;
char arr[100]="This is my string";

printf("These are my output \n car is %d \n String is %s \n", car, string);

or

according to you guys

FILE *stream = fopen("my.txt", "w");
fprintf( stream, "printing text to file\n this is mys string\ %s \n this is car \n %d", string, car );
fclose( stream );


I dont think It will let me do that, but just printing to that external file?
right?

I want be able to print out variables within my print message just like in

printf("These are my output \n car is %d \n String is %s \n", car, string);

can it do that??
what can??

thanks for you knowledge
Junior161
0
Free camera licenses with purchase of My Cloud NAS

Milestone Arcus software is compatible with thousands of industry-leading cameras for added flexibility. Upon installation on your My Cloud NAS, you will receive two (2) camera licenses already enabled in the software. And for a limited time, get additional camera licenses FREE.

 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9785288
You can use sprintf () to print them in a string [char *] and then print this string with fprintf()

HTH

Amit
0
 
LVL 5

Accepted Solution

by:
migoEX earned 100 total points
ID: 9785299
>> fprintf( stream, "printing text to file\n this is mys string\ %s \n this is car \n %d", string, car );

It's OK, and it will print your variables to the files. No extra work needed.
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9785307
The above method is just one more method, but fprintf() would definitely allow you to do with your requirement

HTH

Amit

0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 9785943
fprintf() works just like the printf() does. Only difference is that, it will redirect the output to whichever stream (file pointer) you give it.
You can give STDOUT as stream and see that it prints it on your terminal instaed of external file!
So try this out:
#include <stdio.h>

main()
{
    fprintf(stdout, "printing text to file\n this is mys string\ %s \n this is car \n %d", string, car );
}

If this works (I am sure it will work and you can also verify for your satisfaction), then the other fprintf() will also work and it will write onto the file.
But the only problem that you may face is with file permissions. Check if you have permissions to create files in the same foder as your executable is running!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now