Programação Estruturada 2 - Aula 04 - Código Fonte

7
Programação Estruturada II CÓDIGO FONTE DA AULA 04 ARQUIVOS 2015.1 Prof. Thomás da Costa [email protected]

Transcript of Programação Estruturada 2 - Aula 04 - Código Fonte

Page 1: Programação Estruturada 2 - Aula 04 - Código Fonte

Programação Estruturada II

CÓDIGO FONTE DA AULA 04 ARQUIVOS – 2015.1

Prof. Thomás da Costa [email protected]

Page 2: Programação Estruturada 2 - Aula 04 - Código Fonte

CÓDIGO FONTE DA AULA 04 – ARQUIVOS – 2015.1 Slide 13

#include <iostream> #include <fstream> #include <strings.h> using namespace std; void gravar_arquivo(); int main() { gravar_arquivo(); } void gravar_arquivo() { string valor; valor = "Ola Mundo"; ofstream ofs; ofs.open("ola_mundo.txt", ios::out); ofs << valor; ofs.close(); }

Slide 14

#include <iostream> #include <fstream> #include <strings.h> using namespace std; void ler_arquivo(); int main() { ler_arquivo(); } void ler_arquivo() { string valor; ifstream ifs; ifs.open("ola_mundo.txt", ios::in); if (!ifs.is_open()) { cout << "Não foi possivel abrir o arquivo" << endl; return; } getline(ifs, valor); cout << "Conteúdo do Arquivo:" << valor << endl; ifs.close(); }

Page 3: Programação Estruturada 2 - Aula 04 - Código Fonte

Slide 17

#include <iostream> #include <fstream> #include <strings.h> using namespace std; void gravar_arquivo(); int main() { gravar_arquivo(); } void gravar_arquivo() { int valor_1; double valor_2; float valor_3; char valor_4; cout << "Digite o primeiro numero:" << endl; cin >> valor_1; cout << "Digite o segundo numero:" << endl; cin >> valor_2; cout << "Digite o terceiro numero:" << endl; cin >> valor_3; cout << "Digite o quarto numero:" << endl; cin >> valor_4; ofstream ofs; ofs.open("numeros.txt", ios::out); ofs << valor_1 << endl; ofs << valor_2 << endl; ofs << valor_3 << endl; ofs << valor_4 << endl; ofs.close(); }

Slide 18

#include <iostream> #include <fstream> #include <strings.h> using namespace std; void ler_arquivo(); int main() { ler_arquivo(); } void ler_arquivo() { int valor_1; double valor_2; float valor_3; char valor_4;

Page 4: Programação Estruturada 2 - Aula 04 - Código Fonte

ifstream ifs; ifs.open("numeros.txt", ios::in); if (!ifs.is_open()) { cout << "Não foi possivel abrir o arquivo" << endl; return; } ifs >> valor_1; ifs >> valor_2; ifs >> valor_3; ifs >> valor_4; cout << "Valores do arquivo:" << valor_1 << "," << valor_2 << "," << valor_3 << "," << valor_4; ifs.close(); }

Slide 20

#include <iostream> #include <fstream> #include <strings.h> using namespace std; void gravar_fim_arquivo(); int main() { gravar_fim_arquivo(); } void gravar_fim_arquivo() { string valor; valor = "Nova linha no arquivo"; ofstream ofs; ofs.open("arquivo_linhas.txt", ios::out | ios::app); ofs << valor << endl; ofs.close(); }

Slide 22

#include <iostream> #include <fstream> #include <strings.h> using namespace std; void gravar_arquivo(); int main() { gravar_arquivo(); } void gravar_arquivo() { string valor; ofstream ofs; ofs.open("arquivo_linhas.txt", ios::out);

Page 5: Programação Estruturada 2 - Aula 04 - Código Fonte

valor = "O que são funções:"; ofs << valor << endl; valor = "São rotinas que tem como objetivo, " "executar trechos de códigos de forma modular, " "melhorando a organização do programa e evitando repetição de código."; ofs << valor << endl; valor = "As funções são reutilizáveis dentro de um programa."; ofs << valor << endl; ofs.close(); }

Slide 23

#include <iostream> #include <fstream> #include <strings.h> using namespace std; void ler_arquivo(); int main() { ler_arquivo(); } void ler_arquivo() { string linha; ifstream ifs; ifs.open("arquivo_linhas.txt", ios::in); if (!ifs.is_open()) { cout << "Não foi possivel abrir o arquivo" << endl; return; } while (getline(ifs, linha)) { cout << linha << endl; } ifs.close(); }

Page 6: Programação Estruturada 2 - Aula 04 - Código Fonte

Slide 27

#include <iostream> #include <fstream> #include <strings.h> #include <limits> using namespace std; struct alunos { long id; char nome[255]; char ra[30]; int idade; }; void gravar_arquivo_registros(); int main() { gravar_arquivo_registros(); } void gravar_arquivo_registros() { alunos alu; cout << "Digite o nome do aluno:"; gets(alu.nome); cout << "Digite o RA do aluno:"; gets(alu.ra); cout << "Digite a idade do aluno:"; cin >> alu.idade; fstream fst; fst.open("registros.dat", ios::in | ios::out | ios::app | ios::binary); if (!fst.is_open()) { cout << "Não foi possivel abrir o arquivo" << endl; return; } fst.write((char *)&alu, sizeof(alu)); fst.close(); }

Page 7: Programação Estruturada 2 - Aula 04 - Código Fonte

Slide 29

#include <iostream> #include <fstream> #include <strings.h> #include <limits> using namespace std; struct alunos { long id; char nome[255]; char ra[30]; int idade; }; void ler_arquivo_registros(); int main() { ler_arquivo_registros(); } void ler_arquivo_registros() { alunos alu; fstream fst; fst.open("registros.dat", ios::in | ios::out | ios::app | ios::binary); if (!fst.is_open()) { cout << "Não foi possivel abrir o arquivo"; return; } while (fst.read((char *)&alu,sizeof(alu))) { cout << "************************" << endl; cout << "Nome:" << alu.nome << endl; cout << "RA:" << alu.ra << endl; cout << "Idade:" << alu.idade << endl; } fst.close(); }