Thread: Violation Error

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Violation Error

    Code:
    #include <stdio.h>
    
    int
    function(int num1,int num2)
    {
    int output=0;
    if (num1==num2) output=1;
    return(output);
    }
    
    
    int
    main(void)
    {
    FILE*in;
    int x[25] , y[25], i;
    int num1, num2;
    in = fopen("data2.txt", "r");
    
    
    for (i=0; i<25; ++i)
    		{
    		 fscanf (in, "%d", &num1);
    		 x[i] = num1;
    		}
    		
    		
    for (i=0; i<25; ++i)
    		{
    		 fscanf (in, "%d", &num2);
    		 y[i] = num2;
    		}
    		
    		
    for (i=0; i<25; ++i)
    		{
    		 if(function(x[i],y[i]))
    			printf("The value %d is the same in both data sets at %d subscipt.\n",x[i],i);
    		}
    				 	 
    fclose(in);
    return(0);
    }
    Hey so this is program I have been working on for school and I keep getting a violation error at the first fscanf for some reason. The weird thing is the program ran fine on my school computers but at home I keep getting a violation error. When I ran the program step by step the violation error occurred at " fscanf (in, "%d", &num1);", so I assume the program is having trouble scanning the file for integer numbers or something with scanning the file but I can't seem to make this error go away can anyone help with this? Help would be greatly appreciated, thank you .

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    1) You're not checking to see if the file actually opens before proceeding.
    2) It would be a good idea to give your variables meaningful names...

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Test in to see if it's NULL, maybe the file is not being opened at all.

    Code:
    if(in==NULL) {
      print an error message
      return
    }
    Then check your file's contents - scanf() stops on any error, so a a single wrong char can turn it wonky.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    Hey sorry but how would I go about checking to see if the file actually opens do I check if the file is NULL or is there another way?

    For anyone reading this basically the program opens a file with two lines of integer numbers. There is a total of 50 numbers so I'm suppose to move the first 25 into an array x, and the last 25 into another array y. Then its suppose to go through the file and check if any of the numbers are the same at the exact same spot. I seem to be having trouble opening the file though.

    Example of file:
    01 50 03 45 95 06 08 11 12 19 21 25 24 18 64 32 65 98 54 82 42 12 17 93 53
    00 06 07 18 15 27 29 11 28 31 30 59 61 67 82 41 96 48 27 35 49 36 21 16 81

    basically it would check to see if any of the numbers are the same at the same position, like 11's in the example file.
    Last edited by beatsNstreets; 03-27-2011 at 12:41 PM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You need to scroll up...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock problem
    By Wolf` in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2010, 04:55 PM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

Tags for this Thread