Jump to content
Fórum Script Brasil
  • 0

Por Favor me ajudem


xandyrj

Question

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 to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 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 to comment
Share on other 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 to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...