tiag0 Posted November 4, 2009 at 06:07 PM Report Share #294711 Posted November 4, 2009 at 06:07 PM Boas pessoal, tenho algumas duvidas em programar em C. Tenho um programa que le um ficheiro de texto linha a linha, mas nao sei como separar cada palavra dessa linha e apresentar como strings separadas. Cada palavra do ficheiro está separada por "," (virgula) logo teria que detectar a virgula e separar. Agradeço toda a ajuda. #include <stdio.h> #include <stdlib.h> const int TAM_BUFFER = 255; int main(int argc, char *argv[]) { FILE *arquivo = fopen("nomes.txt", "r"); char buffer[TAM_BUFFER]; if(arquivo != NULL){ while(fgets(buffer, TAM_BUFFER, arquivo)){ printf("%s\n", buffer); } fclose(arquivo); } else printf("Nao foi possivel abrir o arquivo."); printf("\n\n"); system("PAUSE"); return 0; } Aqui está o codigo que ja tenho para apresentar linha a linha do ficheiro. Link to comment Share on other sites More sharing options...
Baderous Posted November 4, 2009 at 06:25 PM Report Share #294716 Posted November 4, 2009 at 06:25 PM Usa a função strtok. Link to comment Share on other sites More sharing options...
S7sRuss Posted November 4, 2009 at 10:05 PM Report Share #294782 Posted November 4, 2009 at 10:05 PM Olha este exemplo retirado do http://www.cplusplus.com/reference/clibrary/cstring/strtok/ /* strtok example */ #include <stdio.h> #include <string.h> int main () { char str[] ="- This, a sample string."; char * pch; printf ("Splitting string \"%s\" into tokens:\n",str); pch = strtok (str," ,.-"); while (pch != NULL) { printf ("%s\n",pch); pch = strtok (NULL, " ,.-"); } return 0; } "Lamento... Detectou mais de dez mil ficheiros infectados..." Link to comment Share on other sites More sharing options...
tiag0 Posted December 8, 2009 at 01:15 AM Author Report Share #299187 Posted December 8, 2009 at 01:15 AM Usei a função strtok mas agr depois de criar um ficheiro nao consigo la introduzir a string que dividi. falta.m declarar cada string numa variavel. obrigado i agradeço ajuda. #include <stdio.h> #include <stdlib.h> const int TAM_BUFFER = 255; // quantidade de caracteres a serem lidos no buffer de cada vez int main(int argc, char *argv[]) { FILE *arquivo = fopen("nomes.txt", "r"); char buffer[TAM_BUFFER]; char[100]=nome1; // testa se o arquivo foi aberto com sucesso if(arquivo != NULL) { // ler o conteúdo do arquivo e armazenar no buffer while(fgets(buffer, TAM_BUFFER, arquivo)) { //ler palavra a palavra char *token = NULL; token=strtok(buffer, "," ); while( token ) { printf("%s\n",token); token = strtok( NULL, "," ); } // criar novos ficheiros { FILE *file = fopen("joao", "w"); //como escrever os nomes dentro do ficheiro?? fprintf(file,"%s\n ",//variavel de cada string); //nome1 seria os nomes que esta no buffer } Link to comment Share on other sites More sharing options...
Baderous Posted December 8, 2009 at 02:20 PM Report Share #299243 Posted December 8, 2009 at 02:20 PM Crias um array de strings onde guardas as strings geradas pelo strtok. Depois percorres o array e escreves as várias strings no ficheiro desejado. 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