?
Solved

Sharing violation error when trying to read from text file

Posted on 2012-04-01
10
Medium Priority
?
554 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
[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
  • 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 2000 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
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!

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

765 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