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
Solved

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

Posted on 2003-11-19
7
260 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

809 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