5. 4) Explique a funcionalidade do programa abaixo:
#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;
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();
}
Resposta:
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
6. 5) Explique a funcionalidade do programa abaixo:
#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();
}
Resposta:
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
7. 6) Explique a funcionalidade do programa abaixo:
#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);
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();
}
Resposta:
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
8. 7) Explique a funcionalidade do programa abaixo:
#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();
}
Resposta:
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
9. 8) Explique a funcionalidade do programa abaixo:
#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();
}
Resposta:
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
10. 9) Explique a funcionalidade do programa abaixo:
#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();
}
Resposta:
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
11. 10) Explique a funcionalidade do programa abaixo:
#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 alterar_registros();
int main()
{
alterar_registros();
}
void alterar_registros()
{
alunos alu;
char ra[30];
char nome[255];
cout << "Digite o RA:" << endl;
gets(ra);
cout << "Digite o novo nome do aluno:" << endl;
gets(nome);
fstream fst;
fst.open("registros.dat", ios::in | ios::out | ios::binary);
if (!fst.is_open())
{
cout << "Não foi possivel abrir o arquivo";
return;
}
int posicao;
while (fst.read((char *)&alu,sizeof(alu)))
{
posicao++;
if (strcmp(ra, alu.ra) == 0)
{
strcpy(alu.nome, nome);
break;
}
}
fst.seekp((posicao - 1) * sizeof(alu));
fst.write((char *)&alu,sizeof(alu));
fst.close();
}