I have an assignment to read a file with the list of months in order and store them which is done at the beginning of the program. 2nd I need to take another file with a list of dates such as
January 17,2004. (one on each line). I need to take that file and add a count/update a count for the that year and month. So that'd be January 2004 that would be updated.
Anyways I initialize the memory of the structure for each month name while reading the first file using malloc. Now I need to add memory to this every time I see this month. I plan on storing the year for each month that comes up into it's own structure and then sorting the years later. I need to add memory as I said so I'm trying to use realloc and when I use this in the for loop
Code:
*month_name = realloc( month_name, (counter2+1)* sizeof(*month_name));
It says assignment makes integer from pointer without a cast (line 84)
when I use
Code:
month_name = realloc( month_name, (counter2+1)* sizeof(*month_name));
It says incompatible types in assignment.
What exactly am I doing wrong on my realloc for memory?
full code is below
Code:
#include <stdio.h>#include <stdlib.h>
#include <string.h>
struct struct_of_months
{
char months[20];
};
struct struct_of_years
{
char year[4];
};
int main(void)
{
FILE *fp;
int counter1 = 0;
int counter2 = 0;
int line_len;
int i=0;
int test;
char buffer[50];
char month_name[50];
char **month_and_year = NULL;
char *month;
char *day;
char *year;
char *p;
struct struct_of_months *array_of_months = malloc (sizeof(*array_of_months));
//Opens the first file and stores the months individually in list_of_months
if ( (fp = fopen("months.txt", "r" )) == NULL )
{
printf("Couldn't open file\n");
exit(1);
}
while(fgets(buffer, sizeof(buffer), fp))
{
//stores months in an array of structures
if (counter1>0)
array_of_months=realloc(array_of_months,(counter1+1)*sizeof(*array_of_months));
strcpy(array_of_months[counter1].months, buffer);
//initializes memory for the years in each month
sprintf(month_name,"%s", month);
struct struct_of_years *month_name = malloc (sizeof(*month_name));
counter1++;
}
fclose( fp );
//Opens the second file and stores the month in month and year in year.
if ( (fp = fopen("dates.csv", "r" )) == NULL )
{
printf("Couldn't open file\n");
exit(1);
}
while(fgets(buffer, sizeof(buffer), fp))
{
month_and_year = realloc(month_and_year, (counter2+1) * sizeof(*month_and_year) );
line_len = strlen(buffer);
month_and_year[counter2] = calloc(line_len+1, sizeof(buffer));
if ((p = strchr(buffer, '\n')) != NULL)
*p = '\0';
//stores month, days, and years
strcpy(month_and_year[counter2],buffer);
month = strtok (month_and_year[counter2], " ");
day = strtok (NULL, ",");
year = strtok (NULL, ",");
//for loops testing each month to the month on that line
for(i = 0; i < counter1; i++)
{
test = strncmp(month, array_of_months[i].months, 3);
sprintf(month_name,"%s", month);
if(test ==0)
month_name = realloc( month_name, (counter2+1)* sizeof(*month_name));
}
counter2++;
}
//frees the memory for the years
for(i = 0; i < counter1; i++)
{
sprintf(month_name,"month-%d", counter1);
free(month_name);
}
fclose( fp );
free(month_and_year);
getchar();
return 0;
}