Solved

Sharing violation error when trying to read from text file

Posted on 2012-04-01
10
538 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
Comment Utility
I'm not getting a compile error or any type of permissions error.
0
 
LVL 8

Expert Comment

by:Anthony2000
Comment Utility
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
Comment Utility
Since the number of items you are looking for in the fscanf is a single "%d", on success fscanf should return 1.
0
 

Author Comment

by:mojeaux
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:mojeaux
Comment Utility
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
Comment Utility
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
Comment Utility
You are correct about reading the numbers in a loop. Does that help?
0
 

Author Comment

by:mojeaux
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now