Rubemlrm Posted June 22, 2012 Report Share Posted June 22, 2012 Boas noites , Venho pedir uma pequena ajuda para resolver um problema num programa que estou a fazer.Só que no processo de abrir um ficheiro e começar a armazenar as informações para popular o vector dos clientes o programa rebenta de todo :S . Deixo aqui o código, atenção esta parte é so a parte que me está a causar problemas. Um nota eu quando tiro o incremento do contador o programa já executa bem. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define MAX 1500 typedef enum disponibilidade {vendido ,disponivel} ESTADO; typedef enum t_comb {gasolina, gasoleo, gas} COMBUSTIVEL; //struct que vai guardar a informação sobre cada carro typedef struct{ char marca[20]; char matricula[15]; char dmatricula[20]; COMBUSTIVEL t_comb; int cilindrada; float preco; ESTADO disponibilidade; }AUTOMOVEL; //struct que vai guardar a informação sobre cada cliente typedef struct{ char nome[50]; char num_ident[9]; char nif[9]; char morada[200]; int tel; }CLIENTE; //struct que irá guardar a informação sobre cada venda typedef struct{ CLIENTE cliente; AUTOMOVEL carro[25]; char dataVenda[8]; float precoFinal; }VENDA; //protóptipos das funções int menu(void); int menucarro(void); int menucliente(void); int menuvenda(void); //protótipos das funões de clientes void editCli(CLIENTE *v_clientes, int nclientes); CLIENTE insertCli(void); void showClient(CLIENTE *v_clientes, int nclientes); //void listallcli(CLIENTE *v_clientes, int nclientes); //protótipos das funções dos carros void listCar(AUTOMOVEL *vautomovel, int ncarros); void showMat(AUTOMOVEL *vautomovel, int i); AUTOMOVEL insertAut(void); void editCar(AUTOMOVEL *vautomovel, int ncarros); //protótipos das funções das vendas void list_vendas(VENDA *vendas, int n_vendas); void v_min(VENDA *vendas, int n_vendas); void v_max(VENDA *vendas, int n_vendas); void t_factura(VENDA *vendas , int n_vendas); //protótipos das funções auxiliares int validarnif(char *numCont); int saveToFile(CLIENTE *v_clientes, int nclientes); int validarNome(char *nome); int validarCC(char *numerocc); int convertAuxDig(char i); int validarMatricula (int dia, int mes, int ano) ; //DECLARAÇÂO DE FUNÇÔES int main() { CLIENTE v_clientes[MAX]; VENDA venda[MAX]; AUTOMOVEL vautomovel[MAX]; int opcao,opcaocarro,opcaocliente,opcaovenda; int n_clientes = 0,n_carros = 0, n_vendas = 0; //Operações para popular os vectores FILE *FILECLI; //FILE *FILEVEN; //FILE *FILECAR; if((FILECLI=fopen("clientes.txt", "r")) == NULL){ printf("Erro:Ficheiro Clientes.txt não existe"); exit(1); } /* if((FILEVEN=fopen("vendas.txt", "r")) == NULL){ printf("Erro:Ficheiro vendas.txt não existe"); exit(1); } if((FILECAR=fopen("carros.txt", "r")) == NULL){ printf("Erro:Ficheiro carros.txt não existe"); exit(1); } */ char *auxiliar; char buffer[255]; char *capt[100]; int i=0; while ((auxiliar = fgets(buffer, 255, FILECLI)) != NULL){ capt[i] = strdup(auxiliar); //este contador está me arrebentar com o programa. i++; } fclose(FILECLI); int y = 0; for (y= 0; y<i; y++) { strcpy(v_clientes[y].nome , strtok (capt[y], "#" )); strcpy(v_clientes[y].nif , strtok (NULL, "#" )); strcpy(v_clientes[y].num_ident,strtok (NULL, "#" )); strcpy(v_clientes[y].morada,strtok (NULL, "#" )); // este strtok é como divide a o texto a cada # v_clientes[y].tel=atoi(strtok (NULL, "#")); } do { opcao=menu(); switch(opcao) { case 1: do{ opcaocarro = menucarro(); if(opcaocarro >= 2 && opcaocarro <= 3 && n_carros == 0){ printf("Impossivel não existem carros registados! "); }else{ switch (opcaocarro) { case 1: vautomovel[n_carros++]=insertAut(); break; case 2: editCar(vautomovel, n_carros); break; case 3: listCar(vautomovel,n_carros); break; case 4: showMat(vautomovel,n_carros); break; case 0: main(); break; default: break; } } }while(opcaocarro < 4); break; case 2: do{ opcaocliente = menucliente(); if(opcaocliente >= 2 && opcaocliente <= 3 && n_clientes == 0){ printf("Impossivel não existem clientes registados"); }else{ switch (opcaocliente) { case 1: if(n_clientes < MAX){ v_clientes[n_clientes++] = insertCli(); }else{ printf("já chegou ao limite de clientes"); } break; case 2: editCli(v_clientes, n_clientes); break; case 3: // listallcli(v_clientes, n_clientes); break; case 4: showClient(v_clientes, n_clientes); break; case 0: saveToFile(v_clientes, n_clientes); break; default: break; } } }while(opcaocliente < 5); break; case 3: opcaovenda = menuvenda(); break; case 0: saveToFile(v_clientes, n_clientes); break; }//switch printf("\n Pressione qualquer tecla para continuar"); //getch(); }while(opcao!=0); return 0; } Link to comment Share on other sites More sharing options...
Guest Posted June 22, 2012 Report Share Posted June 22, 2012 (edited) Boas. O objectivo é fazeres strdup() das frases? Edited June 22, 2012 by Guest Link to comment Share on other sites More sharing options...
seuqram Posted June 22, 2012 Report Share Posted June 22, 2012 (edited) tenta trocar o modo de que o arquivo vai ser aberto de: r(leitura) para r+(leitura/escrita) fica assim #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define MAX 1500 typedef enum disponibilidade {vendido ,disponivel} ESTADO; typedef enum t_comb {gasolina, gasoleo, gas} COMBUSTIVEL; //struct que vai guardar a informação sobre cada carro typedef struct{ char marca[20]; char matricula[15]; char dmatricula[20]; COMBUSTIVEL t_comb; int cilindrada; float preco; ESTADO disponibilidade; }AUTOMOVEL; //struct que vai guardar a informação sobre cada cliente typedef struct{ char nome[50]; char num_ident[9]; char nif[9]; char morada[200]; int tel; }CLIENTE; //struct que irá guardar a informação sobre cada venda typedef struct{ CLIENTE cliente; AUTOMOVEL carro[25]; char dataVenda[8]; float precoFinal; }VENDA; //protóptipos das funções int menu(void); int menucarro(void); int menucliente(void); int menuvenda(void); //protótipos das funões de clientes void editCli(CLIENTE *v_clientes, int nclientes); CLIENTE insertCli(void); void showClient(CLIENTE *v_clientes, int nclientes); //void listallcli(CLIENTE *v_clientes, int nclientes); //protótipos das funções dos carros void listCar(AUTOMOVEL *vautomovel, int ncarros); void showMat(AUTOMOVEL *vautomovel, int i); AUTOMOVEL insertAut(void); void editCar(AUTOMOVEL *vautomovel, int ncarros); //protótipos das funções das vendas void list_vendas(VENDA *vendas, int n_vendas); void v_min(VENDA *vendas, int n_vendas); void v_max(VENDA *vendas, int n_vendas); void t_factura(VENDA *vendas , int n_vendas); //protótipos das funções auxiliares int validarnif(char *numCont); int saveToFile(CLIENTE *v_clientes, int nclientes); int validarNome(char *nome); int validarCC(char *numerocc); int convertAuxDig(char i); int validarMatricula (int dia, int mes, int ano) ; //DECLARAÇÂO DE FUNÇÔES int main() { CLIENTE v_clientes[MAX]; VENDA venda[MAX]; AUTOMOVEL vautomovel[MAX]; int opcao,opcaocarro,opcaocliente,opcaovenda; int n_clientes = 0,n_carros = 0, n_vendas = 0; //Operações para popular os vectores FILE *FILECLI; //FILE *FILEVEN; //FILE *FILECAR; if((FILECLI=fopen("clientes.txt", "r+")) == NULL){ printf("Erro:Ficheiro Clientes.txt não existe"); exit(1); } /* if((FILEVEN=fopen("vendas.txt", "r+")) == NULL){ printf("Erro:Ficheiro vendas.txt não existe"); exit(1); } if((FILECAR=fopen("carros.txt", "r+")) == NULL){ printf("Erro:Ficheiro carros.txt não existe"); exit(1); } */ char *auxiliar; char buffer[255]; char *capt[100]; int i=0; while ((auxiliar = fgets(buffer, 255, FILECLI)) != NULL){ capt[i] = strdup(auxiliar); //este contador está me arrebentar com o programa. i++; } fclose(FILECLI); int y = 0; for (y= 0; y<i; y++) { strcpy(v_clientes[y].nome , strtok (capt[y], "#" )); strcpy(v_clientes[y].nif , strtok (NULL, "#" )); strcpy(v_clientes[y].num_ident,strtok (NULL, "#" )); strcpy(v_clientes[y].morada,strtok (NULL, "#" )); // este strtok é como divide a o texto a cada # v_clientes[y].tel=atoi(strtok (NULL, "#")); } do { opcao=menu(); switch(opcao) { case 1: do{ opcaocarro = menucarro(); if(opcaocarro >= 2 && opcaocarro <= 3 && n_carros == 0){ printf("Impossivel não existem carros registados! "); }else{ switch (opcaocarro) { case 1: vautomovel[n_carros++]=insertAut(); break; case 2: editCar(vautomovel, n_carros); break; case 3: listCar(vautomovel,n_carros); break; case 4: showMat(vautomovel,n_carros); break; case 0: main(); break; default: break; } } }while(opcaocarro < 4); break; case 2: do{ opcaocliente = menucliente(); if(opcaocliente >= 2 && opcaocliente <= 3 && n_clientes == 0){ printf("Impossivel não existem clientes registados"); }else{ switch (opcaocliente) { case 1: if(n_clientes < MAX){ v_clientes[n_clientes++] = insertCli(); }else{ printf("já chegou ao limite de clientes"); } break; case 2: editCli(v_clientes, n_clientes); break; case 3: // listallcli(v_clientes, n_clientes); break; case 4: showClient(v_clientes, n_clientes); break; case 0: saveToFile(v_clientes, n_clientes); break; default: break; } } }while(opcaocliente < 5); break; case 3: opcaovenda = menuvenda(); break; case 0: saveToFile(v_clientes, n_clientes); break; }//switch printf("\n Pressione qualquer tecla para continuar"); //getch(); }while(opcao!=0); return 0; } Edited June 22, 2012 by seuqram Link to comment Share on other sites More sharing options...
Rubemlrm Posted June 22, 2012 Author Report Share Posted June 22, 2012 @seuqram mesmo assim dá segmentation fault. Eu pos um printf para ver o que acontece ao ler o ficheiro e antes do contador incrementar e tenho este output. ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####0 ####32694 /####0 ####32767 ####5 ####0 Ptd##d##32694 #2##P#32694 W##/#{^l#32767 ##/##0 x/#W#/##0 Segmentation fault O segmentation fault é por causa do i++ :/ Link to comment Share on other sites More sharing options...
seuqram Posted June 22, 2012 Report Share Posted June 22, 2012 qual o ficheiro dá erro? Link to comment Share on other sites More sharing options...
bsccara Posted June 22, 2012 Report Share Posted June 22, 2012 De acordo com o que dizes ser um printf no ciclo, o teu ficheiro tem mais de 100 linhas. Mas o teu array 'capt' está dimensionado para 100 ponteiros. A partir da 101ª linha vais escrever fora do array, por cima da pilha, alterando outras variáveis locais e destruindo a 'stack frame'. Isso acaba por provocar a segfault. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now