#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <sys/types.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
char date[35] = {"0"};
void main()
{
FILE *fp;
char ch;
int i=0;
if(fork() == 0)
{
system("date '+%Y-%m-%d %H:%M:%S' > date.txt");
exit(0);
}
wait(0);
fp = fopen("date.txt","w+");
if(fp == 0) {return;}
while((ch = fgetc(fp)) != EOF)
{
date[i++] = (char)ch;
}
fclose(fp);
date[i] = '\0';
printf("date = %s", date);
}
I keep on getting segmentation fault in this code. I have even tried creating the file named "date.txt" manually.
The command date '+%Y-%m-%d %H:%M:%S' > date.txt
works on bash separately well.
Can someone please help me find, what am I doing wrong?
[update:] Forgot to initialize i = 0. Edited but still the segment violation persists.