Solved

Sharing violation error when trying to read from text file

Posted on 2012-04-01
10
548 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
countHi challenge 25 143
xamarin c# deserialize Json containing nested object 2 121
What's the Difference Between a VI, the Command Prompt and a Shell 7 122
Message not shown 5 67
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
This video teaches viewers about errors in exception handling.
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…

840 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