4
Hi, I have a big problem! I have to read two very large numbers of a file and save each position in a list. I’m using fgetc to pick up each element, the two numbers are separated by a blank space, however after I read the first and start the while to pick the second it picks up from the beginning of the line. Would anyone have any idea how to solve this problem?
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
typedef struct celula{
int num;
struct celula *prox;
} celula;
typedef struct pilha{
celula c;
celula *topo, *fundo;
int tamanho;
}pilha;
void fpvazia (pilha *p){
p->topo = (celula*) malloc (sizeof(celula));
p->fundo = p->topo;
p->topo->prox = NULL;
p->tamanho = 0;
}
int vazia(pilha p){
return (p.topo == p.fundo);
}
void insere (pilha *p, int x){
celula *aux;
aux = (celula*) malloc (sizeof(celula));
p->topo->num = x;
aux->prox = p->topo;
p->topo = aux;
p->tamanho++;
}
void retira (pilha *p, int *x){
celula *aux;
if(vazia(*p)){
printf("Erro! Lista vazia!\n");
return;
}
aux = p->topo;
(*x) = aux->prox->num;
free(aux);
p->tamanho--;
}
int tamanho(pilha p){
return (p.tamanho);
}
void imprime(pilha p){
celula *aux;
aux = (celula*)malloc (sizeof(celula));
aux = p.topo->prox;
while(aux!=NULL){
printf("%d", aux->num );
aux = aux->prox;
}
}
int main (){
pilha p, q;
FILE *input;
input = fopen("exemplo.txt", "r");
if (input == NULL){
printf("Erro ao abrir o arquivo\n");
return 0;
}
char c;
int n;
fpvazia(&p);
fpvazia(&q);
//while (!feof(input)){
//while (!feof(input)){
c = fgetc(input);
while (c!=' '){
// if (c == ' ')
// break;
//printf("%c\n", c);
n = c - 48;
printf("%d\n", n );
//printf("%d\n\n", n );
insere(&p, n);
c = fgetc(input);
}
//while (!feof(input)){
//c = fgetc(input);
c= fgetc(input);
while (c!='\n'){
// if (c == ' ')
// break;
//printf("%c\n", c );
n = c - 48;
insere(&q, n);
c = fgetc(input);
}
imprime(q);
//imprime(q);
printf("\n%d\n", tamanho(q));
//printf("%d\n", tamanho(p));
//retira(&l, l.primeiro->prox, &z);
//printf("%d\n",z );
return 0;
}
Post a sample of the file with the data. So it makes it much easier.
– Lacobus
Example of Input 712476924792051427232 742179675108682905639616864728692565
– luiza
in the example the numbers are small and would give salvation with a long long int, but in the statement he says that they can be giant numbers
– luiza