-4

I wrote a code to implement file locking in c. After resolving a errors and ignoring all the warnings haha, I was able to compile the code but now the thing is, When I try to add content to the file, it is showing segmentation error. code is:

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>

char data[1000], ch;
int cont;
int main(int argc, char **argv) {
if (argc > 1) {
int fd = fopen(argv[1], "w");
if(fd == -1) {
  printf("Unable to open the file\n");
  exit(1);
}
static struct flock lock;

lock.l_type = F_WRLCK;
lock.l_start = 0;
lock.l_whence = SEEK_SET;
lock.l_len = 0;
lock.l_pid = getpid();

int ret = fcntl(fd, F_SETLKW, &lock);
printf("Return value of fcntl:%d\n",ret);
printf("\n\n\tOperations you can perform here:\n\t\n");
printf("\n\t1.ADD TO FILE\n\t2.DELETE THE FILE\n\t3.PRINT CONTENTS OF FILE\n\t4.EXIT");
printf("\n\tEnter your choice: ");
scanf("%d",&cont);
switch(cont)
    {
    case 1:
        printf("Enter contents to store in file : \n");
        fgets(data, 1000, stdin);
        fputs(data, fd);
        printf("Data added to the file successfully");
        break;
    case 2:
        remove(fd);
        break;
    case 3:
        int c = getc(fd);
        while (c != EOF)
        {
            printf("%c", c);
            c = getc(fd);
        }
    case 4:
        exit(0);
    }
}
}

I even tried doing

fclose(fd); fd = fopen("advisory.txt","a");

this, at the beginning of the switch case to add content to file. But to no avail.

  • `char c = getc(fd);` c should be an int (plus: don't repeat yourself ...) – wildplasser Dec 20 '20 at 14:53
  • Ok sir, I'll do it. Sir, the error arises in the first case (The one I was talking about :( ) Can you please see my edit and tell if its right (what I did) @wildplasser – HiThereItzMeAgain Dec 20 '20 at 14:57
  • 3
    Please fix the compiler warnings instead of ignoring them. When I compile the program I get warnings that actually indicate errors. You seem to confuse `open` vs. `fopen` and call other functions with wrong arguments. – Bodo Dec 20 '20 at 15:01
  • but can you atleast tell me why this error occurs? Or what could be a possible reason for this error? – HiThereItzMeAgain Dec 20 '20 at 15:04
  • 1
    there no header file like `#include ` also just have a look at [fopen](https://man7.org/linux/man-pages/man3/fopen.3.html) syntax – IrAM Dec 20 '20 at 15:06
  • Yes, sir. I did use both of them without a second thought, then resolved that error by opening the file using fopen(argv[1], "w"); The main concern I have is, Do i have to lose fd using fclose(fd)? and then open th file in append mode again using fopen(argv[1], "a"); ? But i I do that, I am actually closing the fd on which I applied lock. How do I get past this hurdle i.e I need to open the file in append mode to be able to add content to it and still maintain the lock on the file. – HiThereItzMeAgain Dec 20 '20 at 15:10
  • sorry, it was "stdlib" @IrAM and the fopen is also taken care of. Please have a look at the updated code. Thanks for helping – HiThereItzMeAgain Dec 20 '20 at 15:12
  • 1
    Not only are we not your debugger, we are especially not an interactive debugging session. – John Bollinger Dec 20 '20 at 15:14
  • sir, @wildplasser I am not asking for debugging, just that what is causing the segmentation fault(core dumped) error. Please help me – HiThereItzMeAgain Dec 20 '20 at 15:15
  • Sir please don't say this, @JohnBollinger I have been trying to make it work for past 2 days and after scratching my head for soo long, I was not able to figure out the error, I then resorted to this website. I came here because I have faith in your knowledge and know that only you people can help with this. – HiThereItzMeAgain Dec 20 '20 at 15:18
  • The open/fopen confusion is good enough for at least two segmentation faults. (whichever comes first) – wildplasser Dec 20 '20 at 15:19
  • Dear sir @wildplasser I have updated the code after running it again without any success . Do I need to close fd and open fd again in append mode to do this? I have now switched to fopen only because I saw it in your website only that this is better one than open – HiThereItzMeAgain Dec 20 '20 at 15:22

1 Answers1

1

You are confusing file descriptors (as returned by open()) with pointers-to-FILE (as returned by fopen()). Don't put diesel in the tank of a car that needs regular.

Now what?

Read the manual pages for both functions and understand the differences. Understand what is meant by the Standard I/O Library in C and the functions it provides (e.g. fopen(), fgets(), fgetc(), fclose() and many more). Understand what Unix I/O functions are (e.g. open(), read(), write(), close()). Then you will be able to correct the many problems in your code leading to the segfault.

Jens
  • 69,818
  • 15
  • 125
  • 179