I need help, i'm going crazy...
Want to have a function to mark the words of a text file that is not found in a dictionary.The role will have four parameters of type string: the input text file, the file with the dictionary, the text file output and the binary file containing information of the work done.The intention is to revise the words of marking the input file is not in the dictionary supplied in the second parameter and recorded in the file of tasks carried out where and what word has not been found. The function returns 1 if it has performed its task, or 0. This calls for implementing a module composed of the files revisafichero.h and revisafichero.c
To properly identify the words are supplied with a module called "Alfabeto" composed of files alfabeto.c and alfabeto.h This module contains the proceedings inicializaAlfabeto() which we shall call before you start using the module and function enAlfabeto(char)
to which to pass a character returns true if you are in the alphabet, or false, if you're not.
and i hafe this:
Code:
.h
#ifndef REVISAFICHERO_H_
#define REVISAFICHERO_H_
struct InfoNoEncontrada{
char palabra[256];
int linea; /* Número de línea de la palabra. Las líneas comienzan en 1 */
int pos; /* Número de palabra en la línea. Comienzan a contarse en 1 */
};
typedef struct InfoNoEncontrada TFallos;
int revisaFichero(const char *entrada, const char *diccionario, const char *salida, const char *trabajo);
#endif
Code:
.c
#include "revisafichero.h"
#include "alfabeto.h"
#include <string.h>
#include <stdio.h>
int revisaFichero(const char *entrada, const char *diccionario, const char *salida, const char *trabajo){
int palab=0, psalida=0, palab2=0, simb=0, pencontrada=0;
int linea=1, i, j;
char lineas[1000], palabra[256], simbolo[10], pdiccio[256];
char fsal[2000];
FILE *inicio, *fin, *trab, *diccion;
inicializaAlfabeto ();
TFallos fallo;
inicio = fopen(entrada,"r");
if (inicio==NULL) return 0;
//no se puede abrir.
diccion = fopen(diccionario,"r");
if (diccion==NULL) return 0;
//no se abre.
fin = fopen(salida,"w");
if (fin==NULL){
//no se crea.
fclose(inicio);
fclose(diccion);
return 0;
}
trab = fopen(trabajo,"wb");
if (trab==NULL){
//no se crea.
fclose(inicio);
fclose(diccion);
fclose(fin);
return 0;
}
while (!feof(inicio)){ //recorremos el fichero de entrada.
if (fgets(lineas,1000,inicio)){ //vamos extrallendo lineas.
for (i=0; i<=strlen(lineas); i++){//recorremos la linea.
pencontrada=1;
if (enAlfabeto(lineas[i])){
palabra[palab2] = lineas[i];
palab2++;
}
else {
if (lineas[i] == ',' || lineas[i] == '.' ){
simbolo[simb] = lineas[i];
simb++;
}
else{//si no es ni un caracter ni un sombolo es un espacio.
palab++;
palabra[palab2]='\n';//ya tenemos la palabra
//la buscamos en el diccionario.
}
}
while (!feof(diccion)){
if (fgets(pdiccio,256,diccion)){
for (j=0; j<palab2; j++){
pencontrada=1;
if (palabra[j] != pdiccio[j]){
pencontrada=0;
break;
}
}
if (pencontrada == 1){
for (j=0; j<palab2; j++){
fsal[psalida] = palabra[j];
psalida++;
}
if (simb >= 1){
for (j=0; j<simb; j++){
fsal[psalida] = simbolo[j];
psalida++;
}
simb=0; //volvemos a poner la variable a 0.
}
fsal[psalida] = ' ';
psalida++;
palab2=0; //volvemos a poner la variable a 0.
break;
}
}
else{
//no se pudo leer la linea del diccionario.
//cerramos los ficheros.
fclose(inicio);
fclose(fin);
fclose(diccion);
fclose(trab);
return 0;
}
if (pencontrada == 0){//sino encuentra la palabra en el
//diccionario, le añoadimos los corchetes.
fsal[psalida] = '[';
psalida++;
for (j=0; j<psalida; j++){
fsal[psalida] = palabra [j];
psalida++;
}
fsal[psalida] = ']';
psalida++;
if (simb>=1){
for (j=0; j<simb; j++){//añadimos los simbolos.
fsal[psalida] = simbolo[j];
psalida++;
}
simb=0;//ponemos la variable a 0.
}
fsal[psalida] = ' ';//añadimos espacio.
psalida++;
for (j=0; j<=palab2; j++){
fallo.palabra[j]=palabra[j];//usamos una estructura
//para despues pasarlos al fichero de trabajo.
}
fallo.linea=linea;
fallo.pos=palab;
palab2=0;
if (!fwrite(&fallo, sizeof(TFallos), 1, trab)){
fclose(inicio);
fclose(diccion);
fclose(fin);
fclose(trab);
return 0;
}
}
rewind(diccion);//volvemos al princippoo del diccionario.
}
}
}
fsal[psalida-2]='\n';
if (fpintf(fin,"%s",fsal)!=psalida){
fclose(inicio);
fclose(diccion);
fclose(fin);
fclose(trab);
return 0;
}
for (i=0; i<=psalida; i++){
fsal[i]=(char)NULL;
psalida=0;
}
/*else{
fclose(inicio);
fclose(diccion);
fclose(fin);
fclose(trab);
return 0;
}*/
linea++;
palab=0;
}
fclose(inicio);
fclose(diccion);
fclose(fin);
fclose(trab);
return 1;
}