Solved

Writing to a text file in C

Posted on 2012-03-29
3
307 Views
Last Modified: 2012-03-29
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:\\MyOutput.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
0
Comment
Question by:mojeaux
[X]
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
  • 2
3 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 37781302
Hi mojeaux,

you missed to put a {...} around parts of the code:
for(col=1;col<=counter;col++)
      printf("%d ", col);
                  fprintf(fpNumbs, "%d  ", col);
    ...

Open in new window

is interpreted by the compiler as
for(col=1;col<=counter;col++)
{
      printf("%d ", col);
}
                  fprintf(fpNumbs, "%d  ", col);
...

Open in new window


Change the loop to look like this:
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;
}

Open in new window

Hope that helps,

ZOPPO
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 37781335
Sorry, I made a mistake - the printing of 'new line' characters of course has be outside of the for loop's scope:
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;
}

Open in new window

ZOPPO
0
 

Author Closing Comment

by:mojeaux
ID: 37781510
Perfect... I now see my mistake... keeping the new line in the column loop in stead of the row loop.   Thanks so much.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

724 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