Solved

Sharing violation error when trying to read from text file

Posted on 2012-04-01
10
542 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Modbus - whats the maximum I can store in one register? 4 78
word0 challenge 4 65
wordappend challenge 8 147
base64 decode encode 12 121
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
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…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

920 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