Solved

Sharing violation error when trying to read from text file

Posted on 2012-04-01
10
549 Views
Last Modified: 2012-06-27
Hi -
I'm in the learning stages of programming with C.   I have a question about the function for fclose.   My program writes to a text file and then reads from the same file.   In between these two functions, I do close the file.   However, I am getting a sharing violation.   Is there something that I am not doing correctly with the fclose function?

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

int main (void)
{
      /*FUNCTION DECLARATIONS*/
      
      void pyramid(FILE *fpNumbw);
      void oddNumb(FILE *fpNumbr, int *numbIn);
      int readTxt(FILE *fpNumbr, int *numbIn);      
      
      /*LOCAL DECLARATIONS*/

      FILE *fpNumbw;
      FILE *fpNumbr;
      int numbIn;

    /*STATEMENTS*/
      
      fpNumbw=fopen("C:\\MyOutput.txt", "w");

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

      fpNumbr=fopen("C:\\MyOutput.txt", "r");

      if (fpNumbr == NULL)
      {
            printf("Error opening text file!!\n");
            system("pause");
      }
      else
      {
            while (readTxt(fpNumbr, &numbIn))
            {
                  oddNumb(fpNumbr, &numbIn);
                  system("pause");
            }
            fclose(fpNumbr);
      }
      
      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 *fpNumbw)
{
/* LOCAL DECLARATIONS */      
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(fpNumbw, "%d", col);
      }
      fprintf(fpNumbw, "\n");
      printf("\n");
      counter=counter-1;
      }
return;
}/* counter loop*/



/*~~~~~~~~~~~~~~~~~  readTxt Function ~~~~~~~~~~~~~~~
Reads pattern of numbers from MyOutput.txt and then
displays only the odd numbers to the user.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

int readTxt(FILE *fpNumbr, int *numbIn)
{

/* LOCAL DECLARATIONS */      

int ioResult;

/* STATEMENTS */

ioResult = fscanf(fpNumbr, "%d", *numbIn);

if (ioResult == EOF)
      return 0;
else if (ioResult != 9)
{
      printf("Error occurred while reading data\n");
      system("pause");
      return 0;
}
else
      return 1;

}  /*reads from text file */


/*~~~~~~~~~~~~~~~~~  oddNumb Function ~~~~~~~~~~~~~~~
Displays only odd numbers from text file: MyOutput.txt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void oddNumb(FILE *fpNumbr, int *numbIn)
{

/* LOCAL DECLARATIONS */

int oddno;

/* STATEMENTS */

oddno = fscanf(fpNumbr, "%d ", *numbIn);
      
if (oddno % 2 == 0)
            printf(" * ");
      else
            printf("%d ", oddno);
return;


}  /*disyplays odd numbers from text file*/
0
Comment
Question by:mojeaux
  • 6
  • 4
10 Comments
 

Author Comment

by:mojeaux
ID: 37794133
I'm not getting a compile error or any type of permissions error.
0
 
LVL 8

Expert Comment

by:Anthony2000
ID: 37794217
There is nothing wrong with the fclose.

You have a problem with the fscanf, modify the *numberIn to numberIn. You need to pass in the pointer to numberIn not what it points to.

Also, check out this example:

int readTxt(FILE *fpNumbr, int *numbIn)
{

/* LOCAL DECLARATIONS */      

int ioResult;

/* STATEMENTS */

ioResult = fscanf(fpNumbr, "%d", numbIn);

if (ioResult == EOF)
      return 0;
else if (ioResult <= 0) // according to the definition, fscanf will return the number of items that match the pattern or a negative number, please check me on this
{
      printf("Error occurred while reading data\n");
      system("pause");
      return 0;
}
else
      return 1;

}  /*reads from text file */
0
 
LVL 8

Accepted Solution

by:
Anthony2000 earned 500 total points
ID: 37794219
Since the number of items you are looking for in the fscanf is a single "%d", on success fscanf should return 1.
0
Technology Partners: 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!

 

Author Comment

by:mojeaux
ID: 37794266
Thank you for the prompt response.   I made the suggested changes.
 => I removed the * from the fscanf so that its pointing to the value and not the address (this deleted the sharing violation)
 => However, changing the ioResult , <= 0, or to 1, returned a sharing access violation.  

I'm trying to display the numbers that I just wrote to the text file.  Since the numbers are in a pyramid formation, do I need to change the way that file is read so that each line is one less number.   Put it in a loop and de-increment ioResult by 1?

I really appreciate the assist.... I'm struggling with the file IO process.  Thx!
0
 

Author Comment

by:mojeaux
ID: 37794272
ok, caught the second * in the other function and the sharion violation has been resolved.   However, it only returns the 1 (first number in the file).   I'm guessing this means I need to put this in a loop so that it reads every number?
0
 

Author Comment

by:mojeaux
ID: 37794286
I changed the following function:
void oddNumb(FILE *fpNumbr, int *numbIn)
{

/* LOCAL DECLARATIONS */

int oddno;

/* STATEMENTS */

oddno = fscanf(fpNumbr, "%d", numbIn);

while ((fscanf(fpNumbr, "%d", &numbIn)) == 1)
{      
if (oddno % 2 == 0)
            printf(" * ");
      else
            printf("%d ", oddno);
}
return;


However, all I am getting back is 1 1 1 1 1 1 1 1....
0
 

Author Closing Comment

by:mojeaux
ID: 37794301
Thank you.   I will ask another question if I have trouble getting my loop to read the data file
0
 
LVL 8

Expert Comment

by:Anthony2000
ID: 37794310
You are correct about reading the numbers in a loop. Does that help?
0
 

Author Comment

by:mojeaux
ID: 37794323
Yes, I'm using a while statement to read from the text file.   I think I will have another question soon.   I'm getting an error with the odd number formula.   Many thanks for your time and help.
0
 
LVL 8

Expert Comment

by:Anthony2000
ID: 37794337
I just realized that you are using the return value from fscanf as the number you are checking. This is the number of items it was able to find from your format list (i.e. "%d" is only one item). So, when the function returns 1, it means that fscanf found a number that matches the "%d". You need to use numbIn or *numbIn as the value to check for odd or even.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

713 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