Ir para conteúdo
Fórum Script Brasil
  • 0

Por Favor me ajudem


xandyrj

Pergunta

Fazer um programa que lê um arquivo de dados, com registros de tamanho fixo e gera outro, intercalando o primeiro com o "metade + 1", o segundo com o "metade +2", o terceiro com o "metade + 3", ....

Exemplo:

Dado um arquivo de entrada com o conteúdo:

00001

00002

00003

00004

00005

00006

00007

00008

00009

00010

O arquivo de saída deverá ficar:

00001

00006

00002

00007

00003

00008

00004

00009

00005

00010

Link para o comentário
Compartilhar em outros sites

2 respostass a esta questão

Posts Recomendados

  • 0

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define TAMMAXREG 500000


int main(void)
{
   FILE *fp,*fb;
   char *string;
   int tamreg, tamarq;

   /* abrir o arquivo */
   fp = fopen("DEMO.DAT", "r+b");
   if (fp == NULL)
      {
      printf("\nERRO na abertura do arquivo.\n");
      exit(1);
      }

   /* ler a string */
   if (fgets(string, TAMMAXREG, fp) == NULL)
      {
      printf("\nERRO na leitura do arquivo.\n");
      exit(1);


      }
   tamreg = strlen(string);

   if (fseek(fp, 0, SEEK_END) != 0)
      {
      printf("\nERRO no fseek.\n");
      exit(1);
      }

   tamarq= ftell(fp);
   if (tamarq == -1)
      {
      printf("\nERRO no ftell.\n");
      exit(1);
      }

   fb = fopen ("teste.saida","w+");
   if (fb==NULL)
      {
      printf("\nERRO no arquivo de saida.\n");
      exit(1);
      }

   if (fseek(fp,0,SEEK_SET) != 0);
    {
      printf("\nERRO no fseek.\n");
      exit(1);
      }








   /* display the string */

   printf("tamreg = %d\n", tamreg);
   printf("registro = %s\n", string);
   printf("tamarq = %d\n", tamarq);


   fclose(fp);

   getch();
}

Porém ele ainda não está completo,agora tenho que saber como fazer para ler o primeiro registro e grava-lo no arquivo de saida

Link para o comentário
Compartilhar em outros sites

  • 0

#include <string.h>

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#define TAMMAXREG 500000

int main(void)

{

FILE *fp,*fb;

char string [512];

int tamreg, tamarq;

/* abrir o arquivo */

fp = fopen("DEMO.DAT", "r+b");

if (fp == NULL)

{

printf("\nERRO na abertura do arquivo.\n");

exit(1);

}

/* ler a string */

if (fgets(string, TAMMAXREG, fp) == NULL)

{

printf("\nERRO na leitura do arquivo.\n");

exit(1);

}

tamreg = strlen(string);

if (fseek(fp, 0, SEEK_END) != 0)

{

printf("\nERRO no fseek.\n");

exit(1);

}

tamarq= ftell(fp);

if (tamarq == -1)

{

printf("\nERRO no ftell.\n");

exit(1);

}

fb = fopen ("teste.saida","w+b");

if (fb==NULL)

{

printf("\nERRO no arquivo de saida.\n");

exit(1);

}

if (fseek(fp, 0, SEEK_SET) != 0)

{

printf("\nERRO no fseek.\n");

exit(1);

}

if (fread(string,1,tamreg,fp)!=tamreg)

{

printf("\nERRO\n");

exit(1);

}

if (fwrite(string,1,tamreg,fb)!=tamreg)

{

printf("\nERRO\n");

exit(1);

}

if (fseek(fp,5*tamreg,SEEK_SET)!=0)

{

printf("\nERRO no fseek.\n");

exit(1);

}

if (fread (string,1,tamreg,fp)!=tamreg)

{

printf("\nERRO\n");

exit(1);

}

if (fwrite(string,1,tamreg,fb)!=tamreg)

{

printf("\nERRO\n");

exit(1);

}

if (fseek(fp,1*tamreg,SEEK_SET)!=0)

{

printf("\nERRO no fseek.\n");

exit(1);

}

if (fread (string,1,tamreg,fp)!=tamreg)

{

printf("\nERRO\n");

exit(1);

}

if (fwrite(string,1,tamreg,fb)!=tamreg)

{

printf("\nERRO\n");

exit(1);

}

if (fseek(fp,6*tamreg,SEEK_SET)!=0)

{

printf("\nERRO no fseek.\n");

exit(1);

}

if (fread (string,1,tamreg,fp)!=tamreg)

{

printf("\nERRO\n");

exit(1);

}

if (fwrite(string,1,tamreg,fb)!=tamreg)

{

printf("\nERRO\n");

exit(1);

}

if (fseek(fp,2*tamreg,SEEK_SET)!=0)

{

printf("\nERRO no fseek.\n");

exit(1);

}

if (fread (string,1,tamreg,fp)!=tamreg)

{

printf("\nERRO\n");

exit(1);

}

if (fwrite(string,1,tamreg,fb)!=tamreg)

{

printf("\nERRO\n");

exit(1);

}

if (fseek(fp,7*tamreg,SEEK_SET)!=0)

{

printf("\nERRO no fseek.\n");

exit(1);

}

if (fread (string,1,tamreg,fp)!=tamreg)

{

printf("\nERRO\n");

exit(1);

}

if (fwrite(string,1,tamreg,fb)!=tamreg)

{

printf("\nERRO\n");

exit(1);

}

if (fseek(fp,3*tamreg,SEEK_SET)!=0)

{

printf("\nERRO no fseek.\n");

exit(1);

}

if (fread (string,1,tamreg,fp)!=tamreg)

{

printf("\nERRO\n");

exit(1);

}

if (fwrite(string,1,tamreg,fb)!=tamreg)

{

printf("\nERRO\n");

exit(1);

}

if (fseek(fp,8*tamreg,SEEK_SET)!=0)

{

printf("\nERRO no fseek.\n");

exit(1);

}

if (fread (string,1,tamreg,fp)!=tamreg)

{

printf("\nERRO\n");

exit(1);

}

if (fwrite(string,1,tamreg,fb)!=tamreg)

{

printf("\nERRO\n");

exit(1);

}

if (fseek(fp,4*tamreg,SEEK_SET)!=0)

{

printf("\nERRO no fseek.\n");

exit(1);

}

if (fread (string,1,tamreg,fp)!=tamreg)

{

printf("\nERRO\n");

exit(1);

}

if (fwrite(string,1,tamreg,fb)!=tamreg)

{

printf("\nERRO\n");

exit(1);

}

if (fseek(fp,9*tamreg,SEEK_SET)!=0)

{

printf("\nERRO no fseek.\n");

exit(1);

}

if (fread (string,1,tamreg,fp)!=tamreg)

{

printf("\nERRO\n");

exit(1);

}

if (fwrite(string,1,tamreg,fb)!=tamreg)

{

printf("\nERRO\n");

exit(1);

}

/* display the string */

printf("tamreg = %d\n", tamreg);

printf("registro = %s\n", string);

printf("tamarq = %d\n", tamarq);

fclose(fp);

fclose(fb);

getch();

}

Aki está a solução...porém gostaria de colocar em Loop,alguém pode ajudar?

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,2k
    • Posts
      652k
×
×
  • Criar Novo...