Psycop Posted May 7, 2013 at 08:53 PM Report #506158 Posted May 7, 2013 at 08:53 PM (edited) Boas Estou a tentar criar um ficheiro .txt de acordo com o nome para esse ficheiro definido pelo utilizador, mas estou a ter algumas dificuldades em o conseguir. O código que criei para o que pretendo é o seguinte: #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <string.h> typedef struct _UC { char nome_uc[25]; int semestre; int ano; int ects; float nota; }Uc; int main() { Uc *vector_uc; FILE *entrada; int i = 0; int num_linhas = 0; char nome_aluno[100]; char nome_ficheiro_aluno[105]; char txt[5] = ".txt"; //Alocação dinamica de memória para o vector Registo: vector_uc = (Uc *)malloc(10*sizeof(Uc)); if (vector_uc == NULL) { perror("Erro na Allocaçao de Memoria: \n"); exit(1); } else { printf("Memoria alocada com Sucesso:\n\n"); printf("Insira o Nome do Aluno: "); scanf("%s", nome_aluno); nome_ficheiro_aluno = strcat(nome_aluno, txt); //Declaração e abertura do Ficheiro entrada = fopen(nome_ficheiro_aluno, "w+"); if(entrada == NULL) { perror("Erro na Abertura do Ficheiro: \n"); exit(0); } else { printf("Ficheiro Lido com Sucesso:\n\n"); fprintf(entrada, "%s", nome_aluno); } i++; } free(vector_uc); } Alguém me pode dar umas dicas? Cumps Edited May 7, 2013 at 09:00 PM by Psycop
pikax Posted May 7, 2013 at 09:34 PM Report #506160 Posted May 7, 2013 at 09:34 PM Compila? ve como e' que funciona o strcat o strcat retorna um apontador, a menos que uses o strcpy, para copiares para o array, isso nao funciona. Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender. A beleza de um código está em decompor problemas complexos em pequenos blocos simples. "learn how to do it manually first, then use the wizzy tool to save time." "Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast."
Psycop Posted May 7, 2013 at 09:45 PM Author Report #506162 Posted May 7, 2013 at 09:45 PM Pois, fui ver melhor e resolvi desta forma! Cumps printf("Insira o Nome do Aluno: "); scanf("%[^\n]", nome_aluno); strcpy (nome_ficheiro_aluno, nome_aluno); strcat(nome_ficheiro_aluno, ".txt");
pikax Posted May 7, 2013 at 09:48 PM Report #506163 Posted May 7, 2013 at 09:48 PM tens que ter cuidado que o strcpy e' inseguro, pode causar um stack-overflow. desde que nao se meta muitos caracteres, nao ha' problema Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender. A beleza de um código está em decompor problemas complexos em pequenos blocos simples. "learn how to do it manually first, then use the wizzy tool to save time." "Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast."
Psycop Posted May 7, 2013 at 10:01 PM Author Report #506169 Posted May 7, 2013 at 10:01 PM (edited) Bem, tentado desbravar mais um pouco e passando a abertura do ficheiro para uma função estou novamente com problemas! O código é o seguinte: #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <string.h> void criar_aluno(FILE *entrada) { printf("\t\t\t\t***Criar Aluno***\t\t\t\n\n"); char nome_aluno[100]; char nome_ficheiro_aluno[105]; printf("Insira o Nome do Aluno:"); scanf("%[\n]", nome_aluno); printf("\n\n"); strcpy (nome_ficheiro_aluno, nome_aluno); strcat(nome_ficheiro_aluno, ".txt"); //Declaração e abertura do Ficheiro entrada = fopen(nome_ficheiro_aluno, "w+"); if(entrada == NULL) { perror("Erro na Abertura do Ficheiro: \n"); exit(0); } else { } } int main() { int escolha = 1; FILE *entrada; //Menu Principal de Escolha das Opções while (escolha != 5) { printf("\t\t\t\t***Projecto LAB I***\n\n\n"); printf("1 - Criar Novo Aluno: "); printf("\n2 - Editar Informação do Aluno: "); printf("\n3 - Apagar Aluno: "); printf("\n4 - Consultar Informação do Aluno: "); printf("\n5 - Sair: "); printf("\n\nOpçao: "); fflush(stdin); scanf("%d", &escolha); switch(escolha) { case 1: { criar_aluno(entrada); break; } case 2: { break; } case 3: { break; } case 4: { break; } case 5: { break; } default: { break; } } } } Estou a receber um erro de "Argumento Inválido". Cumps Edited May 7, 2013 at 10:01 PM by Psycop
pikax Posted May 7, 2013 at 10:09 PM Report #506174 Posted May 7, 2013 at 10:09 PM //scanf("%[\n]", nome_aluno); scanf("%[^\n]", nome_aluno); //fflush(stdin); scanf("%d", &escolha); fflush(stdin);//limpar o \n --- nunca me lembro se isto e' correcto, penso que nao... ve se fica a funcionar. Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender. A beleza de um código está em decompor problemas complexos em pequenos blocos simples. "learn how to do it manually first, then use the wizzy tool to save time." "Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast."
Psycop Posted May 7, 2013 at 10:13 PM Author Report #506176 Posted May 7, 2013 at 10:13 PM Bem, agora já não dá erro de argumento inválido, mas ao criar o ficheiro este fica com um nome composto por caracteres esquisitos, o que não é o que pretendo! Cumps
pikax Posted May 7, 2013 at 10:16 PM Report #506178 Posted May 7, 2013 at 10:16 PM qual e' o codigo que tens, com essas 2 alterasoes funciona direito aqui. Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender. A beleza de um código está em decompor problemas complexos em pequenos blocos simples. "learn how to do it manually first, then use the wizzy tool to save time." "Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast."
Psycop Posted May 7, 2013 at 10:19 PM Author Report #506179 Posted May 7, 2013 at 10:19 PM Este: #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <string.h> void criar_aluno(FILE *entrada) { printf("\t\t\t\t***Criar Aluno***\t\t\t\n\n"); char nome_aluno[100]; char nome_ficheiro_aluno[105]; printf("Insira o Nome do Aluno:"); fflush(stdin); scanf("%[\n]", nome_aluno); printf("\n\n"); strcpy (nome_ficheiro_aluno, nome_aluno); strcat(nome_ficheiro_aluno, ".txt"); //Declaração e abertura do Ficheiro entrada = fopen(nome_ficheiro_aluno, "w+"); if(entrada == NULL) { perror("Erro na Abertura do Ficheiro: \n"); exit(0); } else { } } int main() { int escolha = 1; FILE *entrada; //Menu Principal de Escolha das Opções while (escolha != 5) { printf("\t\t\t\t***Projecto LAB I***\n\n\n"); printf("1 - Criar Novo Aluno: "); printf("\n2 - Editar Informação do Aluno: "); printf("\n3 - Apagar Aluno: "); printf("\n4 - Consultar Informação do Aluno: "); printf("\n5 - Sair: "); printf("\n\nOpçao: "); scanf("%d", &escolha); fflush(stdin); switch(escolha) { case 1: { criar_aluno(entrada); break; } case 2: { break; } case 3: { break; } case 4: { break; } case 5: { break; } default: { break; } } } }
pikax Posted May 7, 2013 at 10:22 PM Report #506182 Posted May 7, 2013 at 10:22 PM scanf("%[\n]", nome_aluno); Pois, fui ver melhor e resolvi desta forma! Cumps printf("Insira o Nome do Aluno: "); scanf("%[^\n]", nome_aluno); strcpy (nome_ficheiro_aluno, nome_aluno); strcat(nome_ficheiro_aluno, ".txt"); Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender. A beleza de um código está em decompor problemas complexos em pequenos blocos simples. "learn how to do it manually first, then use the wizzy tool to save time." "Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast."
Psycop Posted May 7, 2013 at 10:23 PM Author Report #506183 Posted May 7, 2013 at 10:23 PM Realmente um pequeno erro a estourar com a aplicação! Obrigado! =) Cumps!
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