Jump to content

Duvidas a programar em C


tiag0
 Share

Recommended Posts

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.