wantobegeek
wantobegeek

Reputation: 1725

runtime error (SIGSEGV)

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;
}

Upvotes: 0

Views: 2701

Answers (7)

Tyler McHenry
Tyler McHenry

Reputation: 76660

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.

Upvotes: 2

Stefano Borini
Stefano Borini

Reputation: 143795

$ gcc -Wall test.c -o test
test.c: In function ‘main’:
test.c:8: warning: implicit declaration of function ‘system’
$ ./test 
1 
2
3
10
42
1
2
3
10

Could not reproduce

But yes, I do agree that the system("exit") does not what you expect. What you are exiting from, with that call, is a subshell that is spawned by your program, and then goes on. From the system man page

The system() function hands the argument command to the command interpreter sh(1). The calling process waits for the shell to finish executing the command, ignoring SIGINT and SIGQUIT, and blocking SIGCHLD.

Upvotes: 0

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.

Upvotes: 0

anon
anon

Reputation:

I don't know what you think:

system("exit");

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

exit(1);

Upvotes: 2

1800 INFORMATION
1800 INFORMATION

Reputation: 135285

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");

Upvotes: 1

Simon Nickerson
Simon Nickerson

Reputation: 43159

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 \.

Upvotes: 1

Atmocreations
Atmocreations

Reputation: 10061

  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

Upvotes: 0

Related Questions