0

as stated above I get a seg. fault (core dumped) every time I run the program. What am I doing wrong? My Linux: Manjaro Linux (18.1.4 Juhraya, Kernel 5.4.2.1-MANJARO).

Code 1 (main.c):

#include <ncurses.h>
#include "setup.c"
#include "./src/write.c"
#include "./src/read.c"

int main(){
   int h,w;
   int c;
   char message[128];
   int cursorPos = 0;


   initscr();
   getmaxyx(stdscr, h, w);

   WINDOW *win = newwin(h,w,0,0);

   noecho();
   keypad(stdscr, TRUE);

          wborder(win,CS_BLOCK,ACS_BLOCK,ACS_BLOCK,ACS_BLOCK,ACS_BLOCK,ACS_BLOCK,ACS_BLOCK,ACS_BLOCK);

for(int i = 0; i<w; i++){
    mvwaddch(win, h-3, i, ACS_BLOCK);

}
refresh();
wrefresh(win);


for(;;){
    c = getch();


        wmove(win, h-2, cursorPos+2);

    if(c == 10){
        write(message);
        for(int i = 1; i<w-1; i++){
            mvwaddch(win, h-2, i, ' ');
        }
        for (int i = 0; i < sizeof(message); i++) {
            message[i] = '\0';
        }
        cursorPos = 0;
    }else if(c == 127){
        message[cursorPos] = '\0';
        mvwdelch(win, h-2, cursorPos+1);
        cursorPos -= 1;
    }else  if(c == 27){
        break;
    }

    else{
        message[cursorPos] = c;
            mvwaddch(win, h-2, cursorPos+2, message[cursorPos]);
        cursorPos += 1;
    }
    wrefresh(win);

}

endwin();

return 0;
}

Code 2 (write.c)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

void write(char message[128]) {
  FILE *fptr;
  time_t rawtime;
  struct tm *info;
  char buffer[80];

  time( &rawtime );

  info = localtime( &rawtime );

  strftime(buffer,80,"%x - %I:%M:%p", info);

  fptr = fopen("./chat.txt", "a");
  fprintf(fptr,"%s: %s\n", buffer,message);
  fclose(fptr);
 }

Code 3 (read.c)

#include <stdio.h>

void read()
{
  char c;
  FILE *fptr;
  fptr = fopen("./chat.txt","r");

  if(fptr == NULL)
  {
    printf("Something went wrong");
  }

  c = fgetc(fptr);
  while(c != EOF)
  {
    printf ("%c", c);
    c = fgetc(fptr);
  }
  fclose(fptr);
}

I'm sorry for this long post but this segmentation fault is really bugging me. I appreciate any help. Thank you in advance!

  • 1. Why do you `#include` C files instead of headers? 2. We cannot guess what the exact error message is, so update your question. 3. After updating the question, I suggest you ask it on SO, it might belong better there, and I'll try to assist. – schaiba Dec 17 '19 at 09:44
  • Thank your for your help, I'll post it on SO! – Luca Mathuse Dec 17 '19 at 09:45
  • 4
    You should use a debugger such as `gdb` or `lldb` to narrow down the search to a single line in your source code. – undercat Dec 17 '19 at 09:50
  • 1
    Wild guess - in this: `else if(c == 127)` block, you are assigning `cursorPos` to `-1`. In the next iteration, if you hit the last `else` block, you are trying to access memory you do not own by doing: `message[cursorPos] = c;`. – babon Dec 17 '19 at 10:39
  • regarding: `fptr = fopen("./chat.txt", "a");` Always check (!=NULL) the returned value to assure the operation was successful. If not successful, call `perror()` so both your error message and the text reason the system thinks the error occurred to `stderr`. Then, since the error is not recoverable, call: `exit( EXIT_FAILURE );` Note both `exit()` and `EXIT_FAILURE` are exposed via the `stdlib.h` header file – user3629249 Dec 18 '19 at 04:58
  • Note: the function names: `write()` and `read()` are well known C library functions. It is a poor programming practice to re-write the library functions, Suggest using unique names, like: `myWrite()` and `myRead()` – user3629249 Dec 18 '19 at 05:01
  • regarding: `char c;` and `c = fgetc(fptr);` The the function: `fgetc()` returns an `int` not a `char`. so the `EOF` can be recognized. Suggest: `int c;` – user3629249 Dec 18 '19 at 05:03
  • OT: This: `c = fgetc(fptr); while(c != EOF) { printf ("%c", c); c = fgetc(fptr); }` can be reduced to: `while( (c = fgetc(fptr)) != EOF) { printf ("%c", c); } – user3629249 Dec 18 '19 at 05:06

1 Answers1

1

I recommend the use of valgrind.

Compile with your code with the flag -ggdb3 and then execute valgrind with your program. It'll show you all the invalid reads and writes during the execution of the program. Not only that, it'll tell you exactly in what line they happen and the corresponding function call trace.

This question is a great starting point if you're a beginner to valgrind.

Saucy Goat
  • 1,587
  • 1
  • 11
  • 32