[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 417
  • Last Modified:

Open and write to text file using C

Hi,
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:

#include <stdio.h>
#include <stdlib.h>


   /*DECLARATIONS*/

void pyramid(void);
int openTxt(int);
void Oddnumb(void);
void closeTxt(void);


int main (void)
{
      /*LOCAL DEFINITIONS*/
      FILE* fpNumbs;
            
      /*STATEMENTS*/
      
      fpNumbs = fopen("MyOutput.txt", "w");

      if (fpNumbs == NULL)
      {
                  printf("Error opening text file!!\n");
                  system("pause");
      }
      else
                  pyramid();

      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(void)
{
/* LOCAL DEFINITIONS */      
int row;
int col;
int counter;
FILE* pfNumbs;

/* STATEMENTS */

row=0;
col=0;
counter=9;

for(row=1;row<=9;row++)
{
for(col=1;col<=counter;col++)
      printf("%d ", col);
      fprintf(fpNumbs, ......)
      printf("\n");
{
counter=counter-1;
}
}

}/* 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!!
0
mojeaux
Asked:
mojeaux
  • 3
  • 2
1 Solution
 
mojeauxAuthor Commented:
I believe I found part of my issue... i needed to use \\ in the location ...  for example:
fp=fopen("c:\\test.txt", "r");  

I'm still working to resolve and would love to hear other solutions or comments.   Thanks!
0
 
mojeauxAuthor Commented:
Ok.. now that I'm able to detect the txt file and determine that it's open... how do I write to it from another function.... in the example below, I move the variable to a global position and it still didnt work....

void pyramid(void)
{
/* 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(pfNumbs, "%d", col);
      fprintf(pfNumbs, "\n");
      printf("\n");
{
counter=counter-1;
}
}

}/* counter loop*/
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
In the second code that you posted, you have pfNumbs, but in the first you have fpNumbs? That could be your problem.

If done correctly, it should have worked with it as a global although that wouldn't be recomended. What you should do is leave the FILE* variable local in main, and "pass" it as argument to pyramid. So in main would look like...


.....
FILE* fpNumbs;
           
      /*STATEMENTS*/
     
      fpNumbs = fopen("MyOutput.txt", "w");

      if (fpNumbs == NULL)
      {
                  printf("Error opening text file!!\n");
                  system("pause");
      }
      else
                  pyramid(fpNumbs);
....


And in the function you would have...

void pyramid(FILE* fpNumbs)
{
   ....
   fprintf(fpNumbs, "Blah");
   ....
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
eagerCommented:
You want to define your file handle in one place, such as main or as a global.  If it is global, you can share it with all of the functions.  If you declare it in main,  you need to pass the same file handle to each subroutine.

When you open a file and assign the value returned by fopen to a file handle, the handle points to internal information needed to locate the file.  This is what you did in main(), assigning the file handle value to fpNumbs, a local variable in main().  

When you declare pfNumbs in pyramid(), it is a different variable from the one with the same name in main().  You don't initialize it, so it probably contains a random value.  It definitely doesn't contain the same value as the file handle in main().

You also want to be careful to only open the file in one place, rather than creating multiple file handles pointing to the same file.
0
 
mojeauxAuthor Commented:
Thank you.   This has been most helpful.    I attempted to load a part II to the question yesterday but did not see it listed here.   Still having a couple issues but think they are due to permissions.   Will continue to press on and will post again if not resolved.   Many thanks for the assist.
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Not a problem, glad to help!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now