Solved

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

Posted on 2003-11-19
7
259 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
memory leak detection 9 83
Microsoft vcsetup.exe and vcssetup.exe 8 424
How to design, and implement simple interface 5 135
C simple state machine for client server handshake 4 300
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

773 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