May I impose on you guys again? I'm trying to learn how to write
to disk into a file. I think I got that down, however I would like to
be able to display to screen the same text is being written to file.
What's a way of doing that?. Do I have to repeat the string using "printf"?.
Here's the sample code:
/*
* open_file.c
* trying to create a file.
*
*/
#include <stdio.h>
#define FILE_NAME "C:\\progdir\\openf\\text.txt"
void end_it()
{
printf("\nProgram ended. Press enter to exit: ");
fflush(stdin); /* workes with this compiler */
getchar();
}
int main(void)
{
FILE *file_ptr; /* structure pointer */
atexit(end_it); /* call end_it at anytime it exit */
file_ptr = fopen(FILE_NAME, "w");
if(file_ptr == NULL)
{
fprintf(stderr, "Could not open %s\n",
FILE_NAME);
exit(1);
}
/*
* Test to see if we can write to the file
*/
if (fprintf(file_ptr, "Initial success in writing to file\n\n") < 0)
{
perror("Could not write to the file");
fclose(file_ptr);
exit(1);
}
fprintf(file_ptr, "More writing text here.\nAnd here I keep writing some more\n");
/*
* Close file
*/
fclose(file_ptr);
return(0);
}