?
Solved

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

Posted on 2003-11-19
7
Medium Priority
?
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 
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 300 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

Industry Leaders: 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

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…
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 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.
Suggested Courses

801 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