Ok so I will try one more time, with copying everything without changing even a variables name:
Makros:
Code:
#define ALLOC(P,T,S) if(((P)=(T*)malloc(sizeof(T)*(S))) == NULL) {\
fprintf(stderr,"malloc failed");\
exit(EXIT_FAILURE);\
}
#define REALLOC(P,T,S) if(((P)=(T*)realloc(P,sizeof(T)*(S))) == NULL) {\
fprintf(stderr,"realloc failed");\
exit(EXIT_FAILURE);\
}
My nice not working method.
Code:
void open_file(char *link) {
FILE *file = fopen(link, "r");
if (file == NULL) {
fprintf(stderr, "Could not open file\n");
exit(EXIT_FAILURE);
}
int ge, head_l, seq_num, seq_i, i, read_ll;
char *line, **head, **seqs;
BOOL head_s = FALSE;
ge = 56000;
head_l = 100;
seq_num = 10;
ALLOC(line, char, ge);
ALLOC(head, char*, seq_num);
ALLOC(seqs, char*, seq_num);
for (seq_i = 0; seq_i < seq_num; seq_i++) {
ALLOC(seqs[seq_i], char, ge);
ALLOC(head[seq_i], char, head_l);
}
seq_i = 0;
while (fgets(line, ge, file) != NULL) {
read_ll = strlen(line);
if (line[read_ll - 1] == '\n'
|| (line[read_ll - 1] != '\n' && feof(file))) {
if (line[0] == '>') {
if (head_l < read_ll - 1) {
while (head_l < read_ll - 1) {
head_l *= 2;
REALLOC(head[seq_i], char, head_l);
}
}
memcpy(&head[seq_i], &line, read_ll-1);
// strncpy(head[seq_i], line, read_ll - 1);
head[seq_i][read_ll-1] = '\0';
printf("%s \n", head[seq_i]);
REALLOC(head[seq_i], char, read_ll);
seq_i++;
head_s = TRUE;
} else {
if (!head_s) {
fprintf(stderr, "No header was saved before. Wrong format!\n");
exit(EXIT_FAILURE);
}
strncpy(seqs[seq_i - 1], line, read_ll - 1);
seqs[seq_i - 1][read_ll] = '\0';
head_s = FALSE;
}
} else {
ge *= 2;
REALLOC(line, char, ge);
}
}
/*only deleted part, because It is just doing some stuff to my data*/
FREE(line);
fclose(file);
for (i = 0; i < seq_num; i++) {
printf("%p \n", head[i]);
printf("%p \n", &head[i]);
FREE(head[i]);
FREE(seqs[i]);
}
FREE(head);
FREE(seqs);
}
I am trying to read text file line by line, and extract the first line separately from the other...