Jump to content

Ler inteiro de um ficheiro de texto


Samcity
 Share

Recommended Posts

Boa Tarde.

Estou com umas duvidas de como ler a informação de um ficheiro de texto que posteriormente vai ser guardada numa lista ligada.

Exemplo de informação do ficheiro de texto:

1 2 4 5 7 8

3 5 7

3 4 6 8

2 6 7

A minha dúvida é, como consigo ler inteiro a inteiro até à mudança de linha, tendo em conta que o numero de elementos varia de linha para linha.

*Eu sei o tamanho máximo de linhas e de colunas

Obrigado desde já.

Boa tarde.

Link to comment
Share on other sites

Em 4/25/2015 às 20:08, Samcity disse:

Com listas ligadas não é mais fácil?

não

Em 4/25/2015 às 20:08, Samcity disse:

Não estou a consegui detectar o final de cada linha, não consegue arranjar nenhum exemplo?

#include <stdio.h>

int main(int argc, char** argv) {
int num, run = 1;
char c;

while (run) {
	c = getchar();
	switch(c) {
		case '\n':
			printf("\n");
			break;
		case EOF:
			run = 0;
			break;
		default:
			ungetc(c, stdin);
			if (scanf("%d", &num))
				printf("num:%d ", num);
			break;
	}
}

return 0;
}
IRC : sim, é algo que ainda existe >> #p@p
Link to comment
Share on other sites

Continua a não encontrar o final de cada linha.

O código está assim:


while (run) {
s = getc(f);
switch (s) {
case '\n':
printf("\n");
break;
case EOF:
run = 0;
break;
default:
ungetc(s, f);
if (fscanf(f, "%d", &id_pessoa))
printf("%d\t", id_pessoa);
break;
}
}


Está a ler tudo bem só que não muda de linha.

Input do ficheiro de texto:

1 2 3 4 5 7 8

1 4 6 7

2 3 4 5 6

Output do programa:

1 2 3 4 5 7 8 1 4 6 7 2 3 4 5 6

Edited by apocsantos
geshi
Link to comment
Share on other sites

Sim foi criado em windows.

A função é esta:

A função foi dado pelo professor, porque isto é para um trabalho prático, por isso não deve ser aí o problema, eu só acrescentei a ultima parte, de mudar o nome do ficheiro


int cria_distribuicao(int n_pessoas, int conj, char *txt)
{
int conta[n_pessoas];
int pertence[n_pessoas];
int fixo[n_pessoas];
int i, j, pos, x;
char ficheiro[100];
int number=0, num=1, numero=0;

FILE *f;


if(n_pessoas < 6 || conj < 3 || conj > n_pessoas/2)
{
	printf("Configuracao errada\n");
	return 0;
}

memset(conta, 0, sizeof(conta));
srand((unsigned)time(NULL));

preenche_fixo(fixo, n_pessoas, conj);

f = fopen(txt, "w");
if(f == NULL)
	return 0;

for(i=0; i	{
	memset(pertence, 0, sizeof(pertence));
	x = random_int(2, 3*n_pessoas/conj);

	for(j=0; j			if(fixo[j] == i)
		{
			pertence[j] = 1;
			x--;
		}

	for(j=0; j		{
		do{
			pos = random_int(0, n_pessoas-1);
		}while(pertence[pos]==1 || conta[pos]==2);
		pertence[pos] = 1;
		conta[pos]++;
	}

	for(j=0; j			if(pertence[j] == 1)
			fprintf(f, "%d\t", j+1);
	fprintf(f,"\n");
}

fclose(f);

//MUDA O NOME DO FICHEIRO DE TEXTO
sprintf(ficheiro, "distribuicao%d.txt", num);
//rename(txt, ficheiro);

if ((rename(txt, ficheiro))==-1){

	for (number = 0; number <= 100; number++){

		numero = num + number;

		sprintf(ficheiro, "distribuicao%d.txt", numero);

		if ((rename(txt, ficheiro)) ==0){

			rename(txt, ficheiro);

			printf("\n\nFicheiro guardado com o nome: %s \n", ficheiro);
			printf("\n\n");

return 1;
		}
	}

}
printf("\n\nFicheiro guardado com o nome: %s \n", ficheiro);
printf("\n\n");
return 1;
}


Edited by apocsantos
geshi
Link to comment
Share on other sites

como foi criano no windows, vou assumir que o problema seja a porcaria do '\r' ...

#include <stdio.h>

int main(int argc, char** argv) {
   int num, run = 1;
   char c;

   FILE* fd;
   fd = fopen("input.txt", "r");

   while (run) {
       c = fgetc(fd);
       switch (c) {
           case '\r':
          	 break;
           case '\n':
               printf("\n");
               break;
           case EOF:
               run = 0;
               break;
           default:
               ungetc(c, fd);
               if (fscanf(fd, "%d", &num))
                   printf("num:%d ", num);
               break;
       }
   }

   fclose(fd);

   return 0;
}
IRC : sim, é algo que ainda existe >> #p@p
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.