Hello. Could someone help me with this error (segmentation fault)? I do not know why it's happening.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct cel {
char *palavra;
int numOcorrencias;
} celula;
int charValido(char c) {
if (c != '#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct cel {
char *palavra;
int numOcorrencias;
} celula;
int charValido(char c) {
if (c != '%pre%' && c != ',' && c != '.' && c != '?' && c != '/' && c != '\' && c != '<' && c != '>' && c != '!' && c != '=' && c != '-' && c != '|' && c != '[' && c != ']' && c != '(' && c != ')' && c != '{' && c != '}' && c != 39 && c != '"' && c != ''' && c != '~' && c != '@' && c != '#' && c != '$' && c != '^' && c != '&' && c != '*' && c != ';' && c != ':')
return 1;
return 0;
}
celula *montaDicVet(char *nomeArq, int *tam) {
FILE *arquivo;
celula *vetor;
char *texto, *p, c;
int i, j, num;
i = j = num = 0;
texto = p = NULL;
vetor = NULL;
arquivo = fopen(nomeArq, "r");
while (1) {
c = fgetc(arquivo);
if (c == EOF) {
texto = realloc(texto, ++i);
texto[i-1] = '%pre%';
break;
}
if (charValido(c)) {
i++;
texto = realloc(texto, i);
texto[i-1] = c;
}
}
fclose(arquivo);
i = 0;
while (texto[i] != '%pre%')
printf("%c", texto[i++]);
return vetor;
}
int main(int numargs, char **args) {
celula *vetor;
int *tam = 0;
vetor = montaDicVet(args[1], tam);
printf("%d", *tam);
return 0;
}
' && c != ',' && c != '.' && c != '?' && c != '/' && c != '\' && c != '<' && c != '>' && c != '!' && c != '=' && c != '-' && c != '|' && c != '[' && c != ']' && c != '(' && c != ')' && c != '{' && c != '}' && c != 39 && c != '"' && c != ''' && c != '~' && c != '@' && c != '#' && c != '$' && c != '^' && c != '&' && c != '*' && c != ';' && c != ':')
return 1;
return 0;
}
celula *montaDicVet(char *nomeArq, int *tam) {
FILE *arquivo;
celula *vetor;
char *texto, *p, c;
int i, j, num;
i = j = num = 0;
texto = p = NULL;
vetor = NULL;
arquivo = fopen(nomeArq, "r");
while (1) {
c = fgetc(arquivo);
if (c == EOF) {
texto = realloc(texto, ++i);
texto[i-1] = '%pre%';
break;
}
if (charValido(c)) {
i++;
texto = realloc(texto, i);
texto[i-1] = c;
}
}
fclose(arquivo);
i = 0;
while (texto[i] != '%pre%')
printf("%c", texto[i++]);
return vetor;
}
int main(int numargs, char **args) {
celula *vetor;
int *tam = 0;
vetor = montaDicVet(args[1], tam);
printf("%d", *tam);
return 0;
}
Run as follows: ./testEP4 textSmall.txt
The command line argument refers to the txt file with the following content:
The Polytechnic School of the University of São Paulo (Poli / USP) has more than a century of history, forming generations of engineers who have stood out not only in their professional specialties, but also in the political life of the country and in the administration companies and public bodies. Founded in 1893, the so-called Polytechnic School of São Paulo was incorporated to USP in 1934; today it is a national reference and considered the most complete College of Engineering in Latin America. Poli occupies nine buildings in Cidade Universitária, in São Paulo, in a total of 141,500 square meters of constructed area. There they work or study 457 teachers, 478 employees, 4,500 undergraduates and 2,500 graduate students. The School is organized in 15 departments, responsible for teaching, research and extension services to the community.At the undergraduate level, 17 courses are offered, grouped into four major areas of engineering: Civil, Electrical, Mechanics and Chemistry. Of these courses, 15 are semester and two - Computer Engineering and Chemical Engineering - have characteristics that set them apart from others: they are organized in four-month periods and carried out in cooperation with companies. At the graduate level, Poli offers ten master's degree courses, nine doctorate degrees and a master's degree. From 1970 to 2006, approximately 7,000 titles were awarded, between master's and doctoral degrees, which places the School as one of the largest postgraduate centers in the country and the largest in the area of Engineering. Poli also stands out in the accomplishment of scientific and technological research, with which it contributes to the social and economic progress of the Country and to the modernization, competitiveness and quality of the products and processes of the companies.