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!