Print a list in txt

How I can request the functions on main, because I used different classes. I need to run the functions "EscreveDados" and "LeDados". Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
void EscreveDados(string caminho, ListaEncadeada<Estado> *lista) {
	cout << "Criando arquivo de dados txt" << endl;
	//ofstream escreve(caminho);
	ofstream escreve("arquivo.txt");
	if (!escreve.is_open()) {
		cout << "Falha ao abrir o arquivo" << endl;
		return;
	} else {
		Elemento<Estado> t = lista->pos(0);
		while (t.prox)
			escreve << t.valor << endl;
		escreve.close();
	}
}

void LeDados(ListaEncadeada<Estado> *lista) {
	cout << "Lendo Arquivo" << endl;
	ifstream Ler("arquivo.txt");
	if (Ler.fail()) {
		cout << "Ocorreu um erro na Leitura do Arquivo" << endl;
	}
	Ler.close();
}

int main() {

	portaSerial s; // Cria a classe porta serial
	s.abrir(2); // abre uma conexão na COM2
	ListaEncadeada<Estado> l;
	//EscreveDados(string caminho, ListaEncadeada<Estado> *l);
	Estado e; //TODO passar estado inicial

	Interface abrir;
	//abrir.Menu();
	//abrir.Controle(&s);
	//l.imprime();
	//TODO Criar função para salvar em arquivo
	int a, b;
	c_textcolor(RED);
	cout << "Digite o número de leituras desejado:";
	cin >> a;
	cout << endl << "Escolha o tempo entre cada leitura: ";
	cin >> b;
	ExibeTemperatura g(a, b, &s);
	//g.ExibeTemperatura(a,b);
	l.imprime();

	s.fechar();
	return 0;
}
If you have a ListaEncadeada<Estado> lista; then you can call LeDados like:
LeDados(&lista);

Likewise, you would call EscreveDados like
1
2
string str = "some string";
EscreveDados(str, &lista);


Note: This is merely telling how to call the functional from a point of syntax; no guarantee that this is actually the correct logic that you want.

Prefer const reference over reference, and prefer references over pointers when possible:
1
2
3
4
5
6
7
8
9
10
void LeDados(ListaEncadeada<Estado>& lista) {
    // ...
}

int main()
{
    ListaEncadeada<Estado> lista;
    // ...
    LeDados(lista);
}


By the way, please use better variable names. "l" is a horrible name for a variable, try to make them more than one character.
Last edited on
Thanks for reply, your help is very important. I will test and reply after. Thank you!
Topic archived. No new replies allowed.