I'm trying to write a prog that reads a dictionary and writes the contents to a 2D character array. I'm having a bit of trouble though mainly with passing the array into a function. This compiles, but crashes. I think its because I need to allocate memory in the function or something.
Code:
#include <stdio.h>
#define MAXLEN 30
static FILE *OpenFile(char *file, char *mode);
void LoadDictionary(char *dict[0][0]);
int main()
{
char dictionary[MAXLEN][250000];
LoadDictionary(&dictionary[0][0]);
getchar();
}
void LoadDictionary(char *dict[0][0])
{
char c;
int x=0, y=0;
FILE *in;
in=OpenFile("Dictionary,txt", "r");
while((c=fgetc(in)) != EOF)
{
if(c == '\n')
{
c='\0';
dict[x][y]=c;
y++;
}
else
dict[x][y]=c;
x++;
if(x > MAXLEN-1)x=0;
}
fclose(in);
}
static FILE *OpenFile(char *file, char *mode)
{
FILE *fp = fopen (file, mode);
if (fp == NULL)
{
perror ("Unable to open file");
return NULL;
}
return fp;
}
Also. If, say I wanted to print the first hundred lines from my character array, how would I do that? This is an example of a guess I made, but I doubt it would work:
Code:
for(i=0; i<100; i++)
printf("%s", dictionary[0][i]);