L
leo2100
Hi, I need help with this program. The program is supposed to take a
text file and identify the words in it, then it should print them and
count how many times a word is repeated. At first main called the
function wordcount, and then the function did everything including
printing out the results. That worked. Now I want to make the function
return an array of pointers to struct palabra so the calling function
can manage the data as it pleases. What I`m having trouble with, is to
make the function return a pointer to the array of pointers. I`m using
Dev-C++ compiler and the error says "return from incompatible pointer
type" in the line "return pal;" at the end of the function. I tried a
hundred things but nothing seems to work, if it`s not the same error
it`s "conversion to non-scalar type requested" on the same line. Any
help, as well as tips to make the programa better is welcome and
appreciated.
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUL '\0'
struct palabra
{
char letras[20];
short veces;
};
typedef struct palabra spal;
spal *wordcount(FILE *archivo)
{
char chr, str[20];
struct palabra *pal[1000];
signed short i=0, j=0, k=0, p=0;
for (j = 0; j<20; ++j) str[j] = (char) NUL;
for (i = 0; ; ++i)
{
chr = fgetc(archivo);
switch (chr)
{
case '.':
str = (char) NUL;
i = -1;
break;
case ',':
str = (char) NUL;
i = -1;
break;
case ':':
str = (char) NUL;
i = -1;
break;
case ' ':
str = (char) NUL;
i = -1;
break;
case ';':
str = (char) NUL;
i = -1;
break;
case '!':
str = (char) NUL;
i = -1;
break;
case '?':
str = (char) NUL;
i = -1;
break;
case (char) NUL:
str = (char) NUL;
i = -1;
break;
case EOF:
str = (char) NUL;
i = -1;
break;
default:
str = chr;
break;
}
if (i == -1)
{
if (str[0] != (char) NUL)
{
if (p==0)
{
pal[p] = malloc(sizeof(spal));
pal[p]->veces = 1;
for (j = 0; j<20; ++j) pal[p]->letras[j] =
(char) NUL;
for (j = 0; str[j] != (char) NUL; ++j)
{
pal[p]->letras[j] = str[j];
}
if (p == 1000) printf("El archivo contiene mas
de mil palabras !!!");
++p;
}
else
{
k=0;
for (j = 0; j<p; ++j)
{
if ((strcmp(str, pal[j]->letras)) == 0)
{
++pal[j]->veces;
++k;
}
}
if (k == 0)
{
pal[p] = malloc(sizeof(spal));
pal[p]->veces = 1;
for (j = 0; j<20; ++j) pal[p]->letras[j] =
(char) NUL;
for (j = 0; str[j] != (char) NUL; ++j)
{
pal[p]->letras[j] = str[j];
}
++p;
}
}
for (j = 0; j<= 20; ++j) str[j] = (char) NUL;
}
}
if (chr == EOF) break;
}
if (p==0)
{
printf("El archivo no contiene ninguna palabra\n");
return NULL;
}
else
{
return pal;
/* for (i = 0; i<p; ++i)
{
printf("%s, Veces: %d\n", pal->letras, pal->veces);
}*/
}
}
int main(int argc, char *argv[])
{
FILE *texto;
struct palabra *pal[1000];
short i;
/* if ((texto = fopen("prueba.txt", "w+")) == NULL)
{
printf("Error tratando de abrir el archivo\n");
system("PAUSE");
exit(1);
}
fputs ("Hello World", texto);
if ((fclose(texto)) != 0) printf("No se pudo cerrar correctamente el
archivo\n");*/
if ((texto = fopen("prueba.txt", "r")) == NULL)
{
printf("Error tratando de abrir el archivo\n");
system("PAUSE");
exit(1);
}
if ((pal = wordcount(texto)) == NULL)
{
printf("La operacion fallo\n");
system("PAUSE");
exit(1);
}
for (i = 0; pal->letras[0] != NUL; ++i)
{
printf("%s, Veces: %d\n", pal->letras, pal->veces);
}
if ((fclose(texto)) != NULL) printf("No se pudo cerrar correctamente
el archivo\n");
system("PAUSE");
return 0;
}
text file and identify the words in it, then it should print them and
count how many times a word is repeated. At first main called the
function wordcount, and then the function did everything including
printing out the results. That worked. Now I want to make the function
return an array of pointers to struct palabra so the calling function
can manage the data as it pleases. What I`m having trouble with, is to
make the function return a pointer to the array of pointers. I`m using
Dev-C++ compiler and the error says "return from incompatible pointer
type" in the line "return pal;" at the end of the function. I tried a
hundred things but nothing seems to work, if it`s not the same error
it`s "conversion to non-scalar type requested" on the same line. Any
help, as well as tips to make the programa better is welcome and
appreciated.
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUL '\0'
struct palabra
{
char letras[20];
short veces;
};
typedef struct palabra spal;
spal *wordcount(FILE *archivo)
{
char chr, str[20];
struct palabra *pal[1000];
signed short i=0, j=0, k=0, p=0;
for (j = 0; j<20; ++j) str[j] = (char) NUL;
for (i = 0; ; ++i)
{
chr = fgetc(archivo);
switch (chr)
{
case '.':
str = (char) NUL;
i = -1;
break;
case ',':
str = (char) NUL;
i = -1;
break;
case ':':
str = (char) NUL;
i = -1;
break;
case ' ':
str = (char) NUL;
i = -1;
break;
case ';':
str = (char) NUL;
i = -1;
break;
case '!':
str = (char) NUL;
i = -1;
break;
case '?':
str = (char) NUL;
i = -1;
break;
case (char) NUL:
str = (char) NUL;
i = -1;
break;
case EOF:
str = (char) NUL;
i = -1;
break;
default:
str = chr;
break;
}
if (i == -1)
{
if (str[0] != (char) NUL)
{
if (p==0)
{
pal[p] = malloc(sizeof(spal));
pal[p]->veces = 1;
for (j = 0; j<20; ++j) pal[p]->letras[j] =
(char) NUL;
for (j = 0; str[j] != (char) NUL; ++j)
{
pal[p]->letras[j] = str[j];
}
if (p == 1000) printf("El archivo contiene mas
de mil palabras !!!");
++p;
}
else
{
k=0;
for (j = 0; j<p; ++j)
{
if ((strcmp(str, pal[j]->letras)) == 0)
{
++pal[j]->veces;
++k;
}
}
if (k == 0)
{
pal[p] = malloc(sizeof(spal));
pal[p]->veces = 1;
for (j = 0; j<20; ++j) pal[p]->letras[j] =
(char) NUL;
for (j = 0; str[j] != (char) NUL; ++j)
{
pal[p]->letras[j] = str[j];
}
++p;
}
}
for (j = 0; j<= 20; ++j) str[j] = (char) NUL;
}
}
if (chr == EOF) break;
}
if (p==0)
{
printf("El archivo no contiene ninguna palabra\n");
return NULL;
}
else
{
return pal;
/* for (i = 0; i<p; ++i)
{
printf("%s, Veces: %d\n", pal->letras, pal->veces);
}*/
}
}
int main(int argc, char *argv[])
{
FILE *texto;
struct palabra *pal[1000];
short i;
/* if ((texto = fopen("prueba.txt", "w+")) == NULL)
{
printf("Error tratando de abrir el archivo\n");
system("PAUSE");
exit(1);
}
fputs ("Hello World", texto);
if ((fclose(texto)) != 0) printf("No se pudo cerrar correctamente el
archivo\n");*/
if ((texto = fopen("prueba.txt", "r")) == NULL)
{
printf("Error tratando de abrir el archivo\n");
system("PAUSE");
exit(1);
}
if ((pal = wordcount(texto)) == NULL)
{
printf("La operacion fallo\n");
system("PAUSE");
exit(1);
}
for (i = 0; pal->letras[0] != NUL; ++i)
{
printf("%s, Veces: %d\n", pal->letras, pal->veces);
}
if ((fclose(texto)) != NULL) printf("No se pudo cerrar correctamente
el archivo\n");
system("PAUSE");
return 0;
}