Jump to content

gerar ficheiros em C


tiag0
 Share

Recommended Posts

Boas pesoal,

Ca ando eu as voltas com a programação em C e nao esta facil.

Desta vez a minha duvida consiste em ler um ficheiro linha a linha, e dividir a linha por partes (esta parte ja consegui) e depois de ler gerar varios ficheiros onde possa escrever cada parte da string em cada um desses ficheiros gerados.

Este e o codigo que ja tenho.  Agradeço toda a 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];

  // 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, "," );
  }
}
    fclose(arquivo);
  }
  else
    printf("Nao foi possivel abrir o arquivo.");

  printf("\n\n");
  system("PAUSE");
  return 0;
}
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.