13.07.2015 Views

A Linguagem C Introdução a Programação em Linguagem C ...

A Linguagem C Introdução a Programação em Linguagem C ...

A Linguagem C Introdução a Programação em Linguagem C ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}do{ch = fgetc(fp);//fscanf(fp, "%c", &ch); //igual a ch = fgetc(fp);printf("%c", ch);} while (!feof(fp));fclose(fp);return 0;O seguinte programa faz a leitura de um arquivo texto estruturado <strong>em</strong> 3 colunas, sendo uma de valores inteiros, umacom valores reais e uma com texto.#include int main(void){FILE *fp;char ch;int id1;float id2;char vet[100];1 55.890 teste2 124.44566 ola mundo55 -0.999 xxxx teste}fp = fopen("tabela.txt", "rt"); //abre arquivo para leitura (read/text)do{int cont = fscanf(fp, "%d %f %[^\n]s", &id1, &id2, vet );if( cont == 3 )printf("%d %f %s\n", id1, id2, vet);} while (!feof(fp));fclose(fp);return 0;Para leitura de arquivos binários deve-se saber de ant<strong>em</strong>ão o formado dos dados. Neste formado de arquivo, pode-segravar/ler diretamente tipos básicos ou estruturas complexas.#include #include struct x{int a;char c;float d;};int main(void){FILE *fp;struct x var1, var2;var1.a = 10;var1.c = 'X';var1.d = 24.9980;fp = fopen("a.bin", "wb"); //escrita <strong>em</strong> binário (write/binary)if( fp == NULL ){printf("Erro para criar arquivo");exit(0);}fwrite(&var1, sizeof(struct x), 1, fp);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!