Open and write to text file using C
Posted on 2012-03-27
I'm in the process of learning C. I'm wanting to call a couple functions from the main which will open a text file and then write to the text file. Do I need to declare the FILE* fpNumbs in the main and in both funtions or can I declare it globally? Can I pass the return value from the open back to the calling function to verify that it's open? I'm using Visual Studio 2010 and it seems that when I use the fopen() that it actually creates the text file in the project.... so my validation is not failing when I test. My objective here is to write the number sequence/pyramid to the text file. Here's sample of the code:
int main (void)
fpNumbs = fopen("MyOutput.txt", "w");
if (fpNumbs == NULL)
printf("Error opening text file!!\n");
/*~~~~~~~~~~~~~~~~~ pyramid Function ~~~~~~~~~~~~~~~
Run through 9 rows and 9 columns, each time de-incrementing
the column by one. Display number pattern to user.
/* LOCAL DEFINITIONS */
/* STATEMENTS */
printf("%d ", col);
}/* counter loop*/
This code doesnt reflect my use of openTxt(int)... I've been trying multiple solutions to see what would work... trial and error.... this no workie either. LOL Thanks for the assist!!