I've asked to write code that checks the biggest word in the text file(file1.txt) and write all the words with that size to another text file(file1a.txt) but it says that I have a realloc problem...if I write only one big word in the file1.txt it works but when I write 2 or more it wont pass the reallocation...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
int check_big_word(FILE *read){
int count_letter = 0;
int max_letter_size = 0;
char letter;
letter = fgetc(read);
while(!feof(read) && letter != EOF){
count_letter = 0;
while(!(letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z')){
if (feof(read)){
break;
}
letter = fgetc(read);
}
while((letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z')){
if (feof(read)){
break;
}
count_letter++;
letter = fgetc(read);
}
if( count_letter > max_letter_size ){
max_letter_size = count_letter;
}
}
rewind(read);
return max_letter_size;
}
void print_all_big(FILE *read, FILE *write){
int big_size = check_big_word(read);
int count_letter = 0;
int count_words = 0,i,beggining_of_string = 0;
char letter;
char *string_of_biggers;
string_of_biggers = NULL;
letter = fgetc(read);
while(!feof(read)){
count_letter = 0;
while(!(letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z')){
if (feof(read)){
break;
}
count_words++;
letter = fgetc(read);
}
while((letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z')){
if (feof(read)){
break;
}
count_letter++;
letter = fgetc(read);
}
if(count_letter == big_size){
if(string_of_biggers == NULL){
string_of_biggers = (char*)malloc(sizeof(char)*(big_size+1));
}else{
//******* THE PROBLEM IS HERE *****
string_of_biggers = (char*)realloc(string_of_biggers, sizeof(char)*(big_size+1));
}
rewind(read);
fseek(read,count_words*sizeof(char),SEEK_SET);
for(i = 0; i<big_size;i++){
*string_of_biggers = fgetc(read);
beggining_of_string++;
string_of_biggers++;
}
*string_of_biggers = ' ';
beggining_of_string++;
string_of_biggers++;
}
count_words += count_letter;
}
string_of_biggers = string_of_biggers - beggining_of_string;
fputs(string_of_biggers,write);
fclose(read);
fclose(write);
}
void main(){
FILE *r, *w;
r = fopen("file1.txt", "rt");
w = fopen("file1a.txt", "wt");
print_all_big(r,w);
}
THE GIVEN ERROR IS :
* glibc detected * ./lab14_1: realloc(): invalid pointer: 0x000000000107649c *** ======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7f4c0499fb96] /lib/x86_64-linux-gnu/libc.so.6(realloc+0x28e)[0x7f4c049a489e] ./lab14_1[0x400946] ./lab14_1[0x400a54] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f4c0494276d] ./lab14_1[0x400689] ======= Memory map: ======== 00400000-00401000 r-xp 00000000 00:14 25693062 /home/tailedwiz/Desktop/C_programming/lab14_1 00600000-00601000 r--p 00000000 00:14 25693062 /home/tailedwiz/Desktop/C_programming/lab14_1 00601000-00602000 rw-p 00001000 00:14 25693062 /home/tailedwiz/Desktop/C_programming/lab14_1 01076000-01097000 rw-p 00000000 00:00 0 [heap] 7f4c0470b000-7f4c04720000 r-xp 00000000 08:01 11013974 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f4c04720000-7f4c0491f000 ---p 00015000 08:01 11013974 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f4c0491f000-7f4c04920000 r--p 00014000 08:01 11013974 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f4c04920000-7f4c04921000 rw-p 00015000 08:01 11013974 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f4c04921000-7f4c04ad6000 r-xp 00000000 08:01 11013953 /lib/x86_64-linux-gnu/libc-2.15.so 7f4c04ad6000-7f4c04cd6000 ---p 001b5000 08:01 11013953 /lib/x86_64-linux-gnu/libc-2.15.so 7f4c04cd6000-7f4c04cda000 r--p 001b5000 08:01 11013953 /lib/x86_64-linux-gnu/libc-2.15.so 7f4c04cda000-7f4c04cdc000 rw-p 001b9000 08:01 11013953 /lib/x86_64-linux-gnu/libc-2.15.so 7f4c04cdc000-7f4c04ce1000 rw-p 00000000 00:00 0 7f4c04ce1000-7f4c04d03000 r-xp 00000000 08:01 11013933 /lib/x86_64-linux-gnu/ld-2.15.so 7f4c04ee3000-7f4c04ee6000 rw-p 00000000 00:00 0 7f4c04eff000-7f4c04f03000 rw-p 00000000 00:00 0 7f4c04f03000-7f4c04f04000 r--p 00022000 08:01 11013933 /lib/x86_64-linux-gnu/ld-2.15.so 7f4c04f04000-7f4c04f06000 rw-p 00023000 08:01 11013933 /lib/x86_64-linux-gnu/ld-2.15.so 7fffd5c4d000-7fffd5c6e000 rw-p 00000000 00:00 0 [stack] 7fffd5c9b000-7fffd5c9d000 r-xp 00000000 00:00 0 [vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall] Aborted (core dumped)