Joao Miguel Posted February 24, 2013 at 06:39 AM Report #496819 Posted February 24, 2013 at 06:39 AM (edited) Boa noite. Para um projecto, tenho de numa primeira parte, abrir um ficheiro chama-se "Lista.txt" e dentro deste ficheiro, em cada linha, tem o nome de outros ficheiros que é necessário abrir para depois manipular o conteúdo deles. Contudo, não estou a conseguir abrir os ficheiros que estão na "Lista.txt". Aqui vai o código que fiz: #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_BUF_SIZE 1024 int main() { FILE *f; FILE *new_f; char buf[MAX_BUF_SIZE]; char new_buff[MAX_BUF_SIZE]; f = fopen("Lista.txt", "r"); while(fgets(buf, sizeof(buf), f)) { printf("%s", buf); new_f = fopen(buf, "r"); while(fgets(new_buff, sizeof(new_buff), new_f)); { printf("%s", new_buff); } } return 0; } Não está a funcionar... qualquer tipo de ajuda ou outra forma de fazer é muito bem vinda. Obrigado. Edited February 24, 2013 at 10:43 AM by pmg GeSHi, formatacao
pmg Posted February 24, 2013 at 10:45 AM Report #496823 Posted February 24, 2013 at 10:45 AM (edited) A string lida contem o ENTER final. Esse ENTER não faz parte do nome do ficheiro. Tira o ENTER do buf antes de proseguir while(fgets(buf, sizeof(buf), f)) { buf[strlen(buf) - 1] = 0; printf("%s\n", buf); new_f = fopen(buf, "r"); Edited February 24, 2013 at 10:46 AM by pmg What have you tried? Não respondo a dúvidas por PM A minha bola de cristal está para compor; deve ficar pronta para a semana. Torna os teus tópicos mais atractivos e legíveis usando a tag CODE para colorir o código!
Joao Miguel Posted February 24, 2013 at 04:38 PM Author Report #496862 Posted February 24, 2013 at 04:38 PM (edited) Resultou, thanks. Mas agora o ciclo interior está a ler (fazer o printf) apenas do conteúdo da última linha dos ficheiros. Alguma ideia porquê? Obrigado. (código actualizado): #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { FILE *f; FILE *new_f; char buf[MAX_BUF_SIZE]; char new_buff[MAX_BUF_SIZE]; f = fopen("Lista.txt", "r"); while(fgets(buf, sizeof(buf), f)) { buf[strlen(buf) - 1] = 0; printf("%s \n", buf); new_f = fopen(buf, "r"); while(fgets(new_buff, sizeof(new_buff), new_f)); { new_buff[strlen(new_buff) - 1] = 0; printf("%s \n", new_buff); } } fclose(new_f); fclose(f); return 0; } Não estou a conseguir descobrir o problema... Edited February 24, 2013 at 06:36 PM by pmg GeSHi, formatacao
pmg Posted February 24, 2013 at 06:26 PM Report #496877 Posted February 24, 2013 at 06:26 PM (edited) O teu segundo while não tem corpo 🙂 while (...) ; // while sem corpo por causa do ponto e virgula { // "corpo" desligado /* whatever */ // de qualquer estrutura } // de controle Edited February 24, 2013 at 06:36 PM by pmg What have you tried? Não respondo a dúvidas por PM A minha bola de cristal está para compor; deve ficar pronta para a semana. Torna os teus tópicos mais atractivos e legíveis usando a tag CODE para colorir o código!
Joao Miguel Posted February 24, 2013 at 06:33 PM Author Report #496879 Posted February 24, 2013 at 06:33 PM Oh my god, como é que fui meter um ; ali... se tive-se a compilar com warnings ja tinha visto isso à muito tempo... Para quem vir este post, não se esqueçam de compilar de forma a obter o maior numero de warnings, poupa-vos muito tempo a descobrir onde estão os erros. Muito obrigado. 1 Report
HappyHippyHippo Posted February 24, 2013 at 07:09 PM Report #496883 Posted February 24, 2013 at 07:09 PM Oh my god, como é que fui meter um ; ali... se tive-se a compilar com warnings ja tinha visto isso à muito tempo... Para quem vir este post, não se esqueçam de compilar de forma a obter o maior numero de warnings, poupa-vos muito tempo a descobrir onde estão os erros. Muito obrigado. não tinhas nenhum erro de sintaxe, não existiria nenhum warning para o que se sucedeu IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
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