The program should be able to make an array of numbers from a text file which reads like this
The data is given as this
123 2132 1100909 3213 89890
my code for it is
char a;
char d[100];
char array[100];
a=fgetc(fp) // where fp is a file pointer
if (a=='')
{
d[count1]='/0';
strcpy(&array[count],d);
count=count+1;
memset(d,'\0',100)
count1=0;
}
else
{
d[count1]=a;
count1=count1+1;
}
a=fgetc(fp);
i am getting segmentation fault now . want to store each number in the array so that i can do sorting on it
Your (first) problem is here:
You have written
'/0', which isn’t what you think it is. Assuming you meant'\0'(a nullcharliteral), then you appear to be trying to manually terminate the stringdbefore callingstrcpy(). The problem is that what actually gets written todis not a null byte, and sodis not null-terminated, and thenstrcpy()goes off and starts reading random memory after it, and copying that memory intoarray, until either the reading or the writing ends up outside of memory you’re allowed to access, and you get a segmentation fault.You also have some confusion about that
arrayis. It’s declared as an array of 100chars, but you’re treating it like it’s an array of strings. Perhaps you meant to declare it aschar *array[100]?