Witajcie
Jestem w trakcie pisania programu w C, który ma zczytać nieokreśloną ilość liczb o różnej długości zapisanych w pliku tekstowym. Poniżej prezentuję kod oraz błąd, który pojawia się w konsoli po uruchomieniu poprawnie skompilowanego programu. Czy mógłbym liczyć na pomoc w rozwiązaniu problemu?
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char* getNum (FILE *f)
{
int size=1;
int count;
int len;
char* num;
int c;
num=malloc(sizeof(char)*size);
if (num==NULL) return NULL;
len=size;
count=0;
while (isdigit(c=fgetc(f)))
{
num[count]=c;
count++;
if (count>=len)
{
len+=size;
num=realloc(num,len*sizeof(char));
}
}
ungetc(c,f);
num[count]='\0';
return num;
}
int main (int argc, char* argv[])
{
FILE *in;
char** n;
int size=1;
int count;
int len;
int i;
if (argc!=2) exit(2);
in=fopen(argv[1],"r");
if (!in) exit(3);
n=malloc(sizeof(char)*size);
if (n==NULL) abort();
len=size;
count=0;
while (!feof(in))
{
n[count]=getNum(in);
if (n==NULL)
{
printf ("Malloc failed\n");
break;
}
count++;
if (count>=len)
{
len+=size;
n=realloc(n,len*sizeof(char));
}
n[count]='\0';
}
for (i=0;i<count;i++)
{
printf ("%s",n[count]);
}
free(n);
return 0;
}
*** glibc detected *** ./prog: realloc(): invalid next size: 0x087e8170 ***