tags:

views:

337

answers:

7

hi..can anyone tell me whats wrong in the following program that accepts 1 or 2 digit integers untill it encounters the number 42 after which it prints the previously entered numbers??when i upload this to the sphere online judge site it says compilation successful but runtime error (SIGSEGV).

#include <stdio.h>
int main()
{
  int i;
  FILE *fp;
  fp=fopen("\\db.txt","w+");
  if(fp==NULL)
  {
   printf("file not exitsts and cant be created");
   system("exit");
  }
  while(1)
  {
   scanf("%d",&i);
   if(i==42)
   break;
   else
   {
    fprintf(fp,"%d\n",i); 
   }    
  }
  fclose(fp);
  fp=fopen("\\db.txt","r+");
  if(fp==NULL)
  {
   printf("file not exitsts and cant be created");
   system("exit");
  }
  fscanf(fp,"%d",&i);
  printf("%d\n",i); 
  while((!feof(fp)))
  {
    fscanf(fp,"%d",&i);
    if(!feof(fp))
    printf("%d\n",i);                
  }

  fclose(fp);
  return 0;
}
+3  A: 

I don't know what you think:

system("exit");

will do, but the way to exit a program in C is:

exit(1);
anon
is exit(1) not only for the case of error? a normal exit should be exit(0); right?
Atmocreations
The system("exit") expression in the original is in an error-handling block.
jrockway
C standard specifies three standard error codes: 0, ERROR_SUCCESS and ERROR_FAILURE. Normal exit is 0 or ERROR_SUCCESS.
Simon Nickerson
A: 
  1. at which line do you receive the error?
  2. is your empty #include intended? i think it should mean #include
  3. have you got the error for every input or just for 42?

regards

Atmocreations
+1  A: 

You should replace

system("exit");

with

exit(1);

or, because you're already in main:

return 1;

I suspect the SIGSEGV is caused because you cannot write to the file \\db.txt, but the program continues because system("exit") is not causing it to terminate.

On an semi-related note, SIGSEGV is usually a Unix signal, and path separators on Unix are / rather than \.

Simon Nickerson
+1  A: 

I don't know precisely the cause of the SEGV, but I guess it is because the input doesn't match what you expect. In any case, this line doesn't do what you think it does:

system("exit");
1800 INFORMATION
A: 

SIGSEGV is an access violation error, which indicates a null pointer. Since system("exit") isn't doing anything, fp is getting set to null, and then when you try to use that pointer (for example with fprintf())... boom, your program crashes.

Replace system("exit") with return 1 (or whatever error code you desire), that should fix it.

Ian Kemp
+1  A: 

It seems like you're trying to answer this: http://www.spoj.pl/problems/TEST/ . This problem certainly does not require you to read or write anything from a file, and their server may not allow you to open files on its disk. Even if it does, you're trying to use a windows-style path (with a backslash) on what may be a non-Windows server. And even if it does allow file creation and windows-style path separation, you are trying to create your file in the filesystem root directory, and they almost certainly do not allow file creation there.

Combined with the system("exit") issue that everyone pointed out where it doesn't actually exit the program, this will cause you to receive a NULL file pointer and crash when you try to use it.

Re-read the problem description - you're over-thinking it. It doesn't say anywhere that you have to wait until you get a 42 to print out the other numbers. All you have to do is print back what is entered until you get a 42. That should make the solution much simpler. It's not supposed to be even a mildly challenging problem; it's just supposed to familiarize you with their system.

Tyler McHenry
A: 
Stefano Borini
Try running the program from a directory where the current user does not have permission to create files. It crashes on the first input, as expected.
Tyler McHenry