mojeaux
asked on
Writing to a text file in C
Hi -
I have one more question regarding the writing to a text file. I posted earlier yesterday and resolved the permission issues. The program is working technically... but not as expected. I'm not sure what I've done wrong here... Please view code below and attached files which shows the difference between what the user sees on the display (printf) and what the user sees in the text file (fprintf). I would appreciate a point in the right direction. Thanks!
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
/*LOCAL DEFINITIONS*/
FILE* fpNumbs;
void pyramid(FILE *fpNumbs);
/*STATEMENTS*/
fpNumbs=fopen("C:\\MyOutpu t.txt", "w");
if (fpNumbs == NULL)
{
printf("Error opening text file!!\n");
system("pause");
}
else
pyramid(fpNumbs);
system("pause");
return 0;
}
/*~~~~~~~~~~~~~~~~~ pyramid Function ~~~~~~~~~~~~~~~
Run through 9 rows and 9 columns, each time de-incrementing
the column by one. Display number pattern to user.
~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~*/
void pyramid(FILE *fpNumbs)
{
/* LOCAL DEFINITIONS */
int row;
int col;
int counter;
/* STATEMENTS */
row=0;
col=0;
counter=9;
for(row=1;row<=9;row++)
{
for(col=1;col<=counter;col ++)
printf("%d ", col);
fprintf(fpNumbs, "%d ", col);
fprintf(fpNumbs, "\n");
printf("\n");
{
counter=counter-1;
}
}
}/* counter loop*/
output-different.bmp
I have one more question regarding the writing to a text file. I posted earlier yesterday and resolved the permission issues. The program is working technically... but not as expected. I'm not sure what I've done wrong here... Please view code below and attached files which shows the difference between what the user sees on the display (printf) and what the user sees in the text file (fprintf). I would appreciate a point in the right direction. Thanks!
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
/*LOCAL DEFINITIONS*/
FILE* fpNumbs;
void pyramid(FILE *fpNumbs);
/*STATEMENTS*/
fpNumbs=fopen("C:\\MyOutpu
if (fpNumbs == NULL)
{
printf("Error opening text file!!\n");
system("pause");
}
else
pyramid(fpNumbs);
system("pause");
return 0;
}
/*~~~~~~~~~~~~~~~~~ pyramid Function ~~~~~~~~~~~~~~~
Run through 9 rows and 9 columns, each time de-incrementing
the column by one. Display number pattern to user.
~~~~~~~~~~~~~~~~~~~~~~~~~~
void pyramid(FILE *fpNumbs)
{
/* LOCAL DEFINITIONS */
int row;
int col;
int counter;
/* STATEMENTS */
row=0;
col=0;
counter=9;
for(row=1;row<=9;row++)
{
for(col=1;col<=counter;col
printf("%d ", col);
fprintf(fpNumbs, "%d ", col);
fprintf(fpNumbs, "\n");
printf("\n");
{
counter=counter-1;
}
}
}/* counter loop*/
output-different.bmp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect... I now see my mistake... keeping the new line in the column loop in stead of the row loop. Thanks so much.
you missed to put a {...} around parts of the code:
Open in new window
is interpreted by the compiler asOpen in new window
Change the loop to look like this:
Open in new window
Hope that helps,ZOPPO