5. 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();
}
6. 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();
}