Solved

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

Posted on 2003-11-19
7
257 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

744 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

10 Experts available now in Live!

Get 1:1 Help Now