Organização de Arquivos
Estou com um problema em trabalhar com arquivos em C ...
Tenho um arquivo com alguns registros e estao da seguinte forma (em anexo) (reduzi o tamanho do arquivo para facilitar)
Preciso primeiramente ordenar esse arquivo (baseando-se pelos primeiros 7 campos ), ja tentei de diversas formas
mas ate agora sem resultado
Gostaria que alguem me ajudasse com esse caso.
Segue o codigo do q tenho feito ate entao
#include <stdio.h>
#include <conio.h>
FILE *arq;
char Linha[100];
char *result;
int i;
void ler(){
//clrscr();
// Abre um arquivo TEXTO para LEITURA
arq = fopen("arquivo2.txt", "rt");
if (arq == NULL) // Se houve erro na abertura
{
printf("Problemas na abertura do arquivo\n");
return;
}
i = 1;
while (!feof(arq))
{
// Lê uma linha (inclusive com o '\n')
result = fgets(Linha, 100, arq); // o 'fgets' lê até 99 caracteres ou até o '\n'
if (result) // Se foi possível ler
//printf("Linha %d : %s",i,Linha);
printf("%s",Linha);
i++;
}
fclose(arq);
system("pause");
}
int pesquisar(){
arq = fopen("arquivo3.txt", "rt");
int posicaoAtual = ftell(arq);
//guarda tamanho do arquivo
long tamanho;
// calcula o tamanho
fseek(arq, 0, SEEK_END);
tamanho = ftell(arq);
// recupera o estado antigo do arquivo
fseek(arq, posicaoAtual, SEEK_SET);
int inicio = 0 ;
int fim = tamanho;
int meio = ((inicio+fim)/2)+1;
printf("Inicio %d \n", inicio);
printf("Fim %d \n", fim);
printf("Meio %d \n", meio);
fseek(arq, meio, SEEK_SET);
result = fgets(Linha, 88, arq);
if (result) // Se foi possível ler printf("Linha %d : %s",i,Linha);
printf("%s",Linha);
system("pause");
}int ordenar(){
arq = fopen("arquivo2.txt", "rt");
i = 1;
while (!feof(arq))
{
// Lê uma linha (inclusive com o '\n')
result = fgets(Linha, 100, arq); // o 'fgets' lê até 99 caracteres ou até o '\n'
if (result) // Se foi possível ler
//printf("Linha %d : %s",i,Linha);
printf("%s",Linha);
i++;
}
system("pause");
}
int menu() {
int opcao = 0;
//printf("%d", sizeof(int)+sizeof(int)+sizeof(float)+sizeof(char[40]));
printf("\n-------------------------------------------------------------");
printf("\nAplicação manipulação arquivos em C para funcionarios !! ");
printf("\n-------------------------------------------------------------");
printf("\n1 - Mostra arquivo ----");
printf("\n2 - Pesquisar ---");
printf("\n3 - Ordenar ---");
printf("\n0 - Encerrar Aplicação --------------------------------------");
printf("\nInforme a opção desejada -----------------------------------:");
scanf("%d", &opcao);
return opcao;
}
int main(){
int opcao = menu();
switch(opcao){
case 1:
ler();
break;
case 2:
pesquisar();
break;
case 3:
ordenar();
break;
default:
break;
}
}
segue abaixo registros do arquivo
3192407Measurement of weak radioactivity Pall Theodorsson 41996
3467070How to draw portraits in colored pencil from Lee Hammond 11997
3389270Basic electronics math Clyde N Herrick 51997
0062004Swift and effective retribution Joseph T Stanik 41996
1803239The Naval Institute guide to naval writing Robert Shenk 21997
3063661Gauguins intimate journals Paul Gauguin 61997
6743242ferns of Britain and Ireland C N Page 21997
4280103Black demographic data 17901860 Clayton E Cramer 41997
3992693Inside edge Christine Brennan 11997
0528171Practical spirituality John Randolph Price 51996
5195701Direct instruction reading Douglas Carnine 31997
4353487House of Lords Official Report Parliamentary Hansard 32006
6688800introduction to continental philosophy West David 51996
6820393introduction to aesthetics Dabney Townsend 71997
3467069riesgo en el comercio hispanoindiano Oscar Cruz Barney 11998
4146448commercialisation of English society 100015 Richard H Britnell 21996
2300844Cowboy justice Jim Gober 61997
1666018The cape of Don Francisco Torquemada Benito Perez Galdos 61996
1036106burning of Los Angeles Samuel Maio 51997
4751677The hiding place Corrie Ten Boom 11997
6370785Tolstoy woman and death David Holbrook 61997
0139040The savvy womans success bible Tina Santi Flaherty 11997
5697717Subjectivity and intersubjectivity in modern Roger Frie 51997
5104801Land use practice and forms Delaney John J 21998
2223618Evangelicals and politics in antebellum AmeriRichard Carwardine 61997
1394098Confirmed in Christ Roy G Gesch 61997
2570114Murphys trail Gary Paulsen 81997
Discussão (0)
Carregando comentários...