$ ls
baby.txt readlyrics.c
I tried to write a simple program to print the text from a .txt file using nanosleep() to get some sort of animated effect:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
char *target_file = argv[1];
char *sec = argv[2];
char *nsec = argv[3];
int tv_sec = atoi(sec);
float tv_nsec = atof(nsec);
struct timespec *t;
t->tv_sec = tv_sec;
t->tv_nsec = (long)(tv_nsec * 1000000000);
FILE *content = fopen(target_file, "r");
int *c = malloc(sizeof(char));
c = NULL;
c = fgetc(content);
while(c) {
printf("%c", c);
c = NULL;
nanosleep(t, NULL);
c = fgetc(content);
}
fclose(content);
return 0;
}
And got an error:
$ ./read ./baby.txt 0 0.01
zsh: segmentation fault ./read ./baby.txt 0 0.01
Which part of the code went wrong?