micront Posted October 24, 2009 at 11:29 PM Report Share #293261 Posted October 24, 2009 at 11:29 PM como faço para abrir um ficheiro e colocar o seu conteudo no ecra,ando farto de dar voltas e nao consigo Link to comment Share on other sites More sharing options...
zecapistolas Posted October 25, 2009 at 12:14 AM Report Share #293267 Posted October 25, 2009 at 12:14 AM Começa por aqui.... http://www.cplusplus.com/reference/clibrary/cstdio/fopen/ Link to comment Share on other sites More sharing options...
micront Posted October 25, 2009 at 04:50 PM Author Report Share #293298 Posted October 25, 2009 at 04:50 PM esse programa ao correr cria um novo ficheiro,eu cria abrir um ficheiro ja existente e ao mesmo tempo que o abra para se poder ver o que tem la escrito Link to comment Share on other sites More sharing options...
Baderous Posted October 25, 2009 at 05:03 PM Report Share #293301 Posted October 25, 2009 at 05:03 PM Tens de usar a função fopen para abrir o ficheiro e de seguida usas a fgets para ler o conteúdo linha a linha e imprimes no monitor com printf. Link to comment Share on other sites More sharing options...
micront Posted October 25, 2009 at 05:07 PM Author Report Share #293302 Posted October 25, 2009 at 05:07 PM int main() { FILE * pFile; char buffer [100]; pFile = fopen ("myfile.txt" , "r"); if (pFile == NULL) perror ("Error opening file"); else { while ( ! feof (pFile) ) { fgets (buffer , 100 , pFile); fputs (buffer , stdout); } fclose (pFile); } return 0; Ja agora podiam ajudar a compreender estas linhas de codigo,sou iniciante e estou com alguma dificuldade em entender principalmente a parte do ciclo while Link to comment Share on other sites More sharing options...
Baderous Posted October 25, 2009 at 05:29 PM Report Share #293306 Posted October 25, 2009 at 05:29 PM Enquanto não chegar ao fim do ficheiro apontado pelo pFile (vê a documentação da função feof), lê do ficheiro 100 caracteres para um buffer (uma string), e imprime esses mesmos caracteres no ecrã. Por fim, fecha o ficheiro. Link to comment Share on other sites More sharing options...
micront Posted October 25, 2009 at 07:04 PM Author Report Share #293320 Posted October 25, 2009 at 07:04 PM obrigado 😛 Link to comment Share on other sites More sharing options...
micront Posted November 8, 2009 at 12:33 AM Author Report Share #295094 Posted November 8, 2009 at 12:33 AM FILE * pFile; char string [100]; pFile = fopen ("Lista P.Erdos" , "r"); if (pFile == NULL) perror ("Erro ao abrir o ficheiro"); else { fgets (string , 100, pFile); puts (string); fclose (pFile); } return 0; fiz este programa para ler linha a linha do ficheiro mas nao estou a conseguir alguem me pode ajudar Link to comment Share on other sites More sharing options...
Baderous Posted November 8, 2009 at 12:35 AM Report Share #295095 Posted November 8, 2009 at 12:35 AM Tens de meter um while a englobar as 2 primeiras instruções do else. Link to comment Share on other sites More sharing options...
micront Posted November 8, 2009 at 08:08 PM Author Report Share #295172 Posted November 8, 2009 at 08:08 PM qual a condiçao que fica dentro do while Link to comment Share on other sites More sharing options...
micront Posted November 8, 2009 at 08:19 PM Author Report Share #295173 Posted November 8, 2009 at 08:19 PM ja consegui mas se tiver mais do que uma palavra na mesma linha e quiser meter essas palavras em linhas diferentes como faço. Vou escrever um exemplo para se perceber melhor: joao,carlos,jose,joaquim. output joao carlos jose joaquim Link to comment Share on other sites More sharing options...
Baderous Posted November 8, 2009 at 09:58 PM Report Share #295186 Posted November 8, 2009 at 09:58 PM Tens de fazer parsing dessa linha, podes usar a função strtok para esse efeito. Link to comment Share on other sites More sharing options...
micront Posted November 8, 2009 at 10:19 PM Author Report Share #295190 Posted November 8, 2009 at 10:19 PM como insiro essa funçao no codigo que coloquei anteriormente Link to comment Share on other sites More sharing options...
Baderous Posted November 8, 2009 at 10:46 PM Report Share #295191 Posted November 8, 2009 at 10:46 PM Já tens o while feito? É dentro do while, depois do fgets. Link to comment Share on other sites More sharing options...
micront Posted November 8, 2009 at 10:47 PM Author Report Share #295192 Posted November 8, 2009 at 10:47 PM ja andei a experimentar e nada nao consigo por a funcionar com essa funçao Link to comment Share on other sites More sharing options...
micront Posted November 8, 2009 at 11:52 PM Author Report Share #295197 Posted November 8, 2009 at 11:52 PM dentro do while fiz o seguinte: else { while(fgets (string , 100, pFile)) puts(string); } fclose (pFile); a seguir a while(fgets (string , 100, pFile)) coloco a funçao strtok Link to comment Share on other sites More sharing options...
micront Posted November 8, 2009 at 11:59 PM Author Report Share #295199 Posted November 8, 2009 at 11:59 PM fiz o seguinte e da me erro segment fault FILE * pFile; char string [100]; pFile = fopen ("Lista P.Erdos" , "r"); if (pFile == NULL) perror ("Erro ao abrir o ficheiro"); else { while(fgets (string , 100, pFile),pFile!=NULL) { printf("%s\n",pFile); pFile=strtok(NULL,",.-"); puts(string); } fclose (pFile); } Link to comment Share on other sites More sharing options...
Baderous Posted November 9, 2009 at 12:15 AM Report Share #295204 Posted November 9, 2009 at 12:15 AM Isso nem compila. Aconselho-te a estudares mais: http://www.cplusplus.com/reference/clibrary/cstdio/fgets/ http://www.cplusplus.com/reference/clibrary/cstring/strtok/ 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