Junior161
asked on
I need to know how to print to external files in C language!! Easy question for pros.
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
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
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
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
ASKER
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
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
You can use sprintf () to print them in a string [char *] and then print this string with fprintf()
HTH
Amit
HTH
Amit
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The above method is just one more method, but fprintf() would definitely allow you to do with your requirement
HTH
Amit
HTH
Amit
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!
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!
FILE *stream = fopen("my.txt", "w");
fprintf( stream, "printing text to file\n" );
fclose( stream );