D
dddddddd2444444
Hi,please help...
It works fine when I define a 2-D array like char code[ROWS][SIZE].
But it won't work when I try to define the array dynamically using a
function. It just crashes.
Does anyone know why?
The compiler i'm using is Dev c++.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int file_length(FILE *file);
void down_string(char *p);
char* issubstring(char *str1,char *str2);
char** dynamic_string_array(int ROWS, int SIZE);
int main(void)
{
int row,n,i,coursefound=0,ROWS,SIZE,test;
FILE *datafile;
datafile=fopen("3rdyear.csv", "rb");
ROWS=file_length(datafile);
printf("Has %d lines\n",ROWS);
char **Code,**Course,**ClassSize,**Time1,**Time2,**Room,c;
char search[30];
Code=dynamic_string_array(ROWS,SIZE);
Course=dynamic_string_array(ROWS,SIZE);
ClassSize=dynamic_string_array(ROWS,SIZE);
Time1=dynamic_string_array(ROWS,SIZE);
Time2=dynamic_string_array(ROWS,SIZE);
Room=dynamic_string_array(ROWS,SIZE);
for (row=0; row <ROWS; row++) {
test=fscanf(datafile, "%[^,],%[^,],%[^,],%[^,],%[^,],%[^\n]\n",
Code[row],Course[row],ClassSize[row],
Time1[row],Time2[row],Room[row]);
printf("row=%d,scanf converted %d (%s,%s,%s,%s,%s,%s)\n",
row, test,
Code[row],Course[row],ClassSize[row],Time1[row],Time2[row],Room[row]);
}
printf("Please enter a name\n>");
scanf("%s",search);
for(i=1; i<ROWS; i++) {
if(issubstring(Course,search)) {
coursefound=1;
printf("Course %s found!!\nTime 1 is %s\nTime2 is
%s\nVenue:%s",Course,Time1,Time2,Room);
}
}
if(coursefound==0) printf("No such Course!.\n");
fclose(datafile);
return EXIT_SUCCESS;
}
void down_string(char *p) //turns uppercase letters in a string to
lowercase
{
int i;
for(i=0;p!='\0';i++)
{
if((p>='A')&&(p<='Z')) p+=32;
}
}
char* issubstring(char *str1,char *str2) //checks if string2 is a
substring of string2
{
char tmp1[30],tmp2[30];
strcpy(tmp1, str1);
strcpy(tmp2, str2);
down_string(tmp1); //turn it to lowercase
down_string(tmp2);
return (strstr(tmp1, tmp2));
}
char** dynamic_string_array(int ROWS, int SIZE)
{
char **array;
int i;
array=(char**) malloc(ROWS*sizeof(char));
for(i=0;i<ROWS;i++)
array=(char *) malloc(SIZE*sizeof(char));
return array;
}
int file_length(FILE *file)
{
int lines;
char dummy[100];
rewind(file);
lines=0;
while( fgets(dummy, 100, file) != NULL)
lines++;
rewind(file);
return(lines);
}
It works fine when I define a 2-D array like char code[ROWS][SIZE].
But it won't work when I try to define the array dynamically using a
function. It just crashes.
Does anyone know why?
The compiler i'm using is Dev c++.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int file_length(FILE *file);
void down_string(char *p);
char* issubstring(char *str1,char *str2);
char** dynamic_string_array(int ROWS, int SIZE);
int main(void)
{
int row,n,i,coursefound=0,ROWS,SIZE,test;
FILE *datafile;
datafile=fopen("3rdyear.csv", "rb");
ROWS=file_length(datafile);
printf("Has %d lines\n",ROWS);
char **Code,**Course,**ClassSize,**Time1,**Time2,**Room,c;
char search[30];
Code=dynamic_string_array(ROWS,SIZE);
Course=dynamic_string_array(ROWS,SIZE);
ClassSize=dynamic_string_array(ROWS,SIZE);
Time1=dynamic_string_array(ROWS,SIZE);
Time2=dynamic_string_array(ROWS,SIZE);
Room=dynamic_string_array(ROWS,SIZE);
for (row=0; row <ROWS; row++) {
test=fscanf(datafile, "%[^,],%[^,],%[^,],%[^,],%[^,],%[^\n]\n",
Code[row],Course[row],ClassSize[row],
Time1[row],Time2[row],Room[row]);
printf("row=%d,scanf converted %d (%s,%s,%s,%s,%s,%s)\n",
row, test,
Code[row],Course[row],ClassSize[row],Time1[row],Time2[row],Room[row]);
}
printf("Please enter a name\n>");
scanf("%s",search);
for(i=1; i<ROWS; i++) {
if(issubstring(Course,search)) {
coursefound=1;
printf("Course %s found!!\nTime 1 is %s\nTime2 is
%s\nVenue:%s",Course,Time1,Time2,Room);
}
}
if(coursefound==0) printf("No such Course!.\n");
fclose(datafile);
return EXIT_SUCCESS;
}
void down_string(char *p) //turns uppercase letters in a string to
lowercase
{
int i;
for(i=0;p!='\0';i++)
{
if((p>='A')&&(p<='Z')) p+=32;
}
}
char* issubstring(char *str1,char *str2) //checks if string2 is a
substring of string2
{
char tmp1[30],tmp2[30];
strcpy(tmp1, str1);
strcpy(tmp2, str2);
down_string(tmp1); //turn it to lowercase
down_string(tmp2);
return (strstr(tmp1, tmp2));
}
char** dynamic_string_array(int ROWS, int SIZE)
{
char **array;
int i;
array=(char**) malloc(ROWS*sizeof(char));
for(i=0;i<ROWS;i++)
array=(char *) malloc(SIZE*sizeof(char));
return array;
}
int file_length(FILE *file)
{
int lines;
char dummy[100];
rewind(file);
lines=0;
while( fgets(dummy, 100, file) != NULL)
lines++;
rewind(file);
return(lines);
}