so I'm trying to read some data from a text file, then save that data in a struct, and then output that data to another text file with a different name. The problem I have is that the output file is blank, also tried putting some cout to see if the code is running properly but get no output to the console. I assume it's something related to ofstream because when I try to identify the problem I remove every line related with ofstream and output the data I read to the console and it works. I code in spanish hope there's no problem with that, here's the code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdio.h>
#include <string>
using namespace std;
typedef char str20[20];
struct sPaises {
str20 nom,
cont;
int hab;
};
bool abrirArchivos(ifstream &archE, sPaises &rPais){
archE >> rPais.nom;
archE >> rPais.cont;
archE >> rPais.hab;
return archE.good();
}
int main(){
sPaises rPaises;
ifstream paisesE("paises.txt");
ofstream lstPaises("ListadoPaises.txt");
while (abrirArchivos(paisesE, rPaises) == 1){
lstPaises << rPaises.nom << rPaises.cont << rPaises.hab << endl;
}
cout << paisesE.good() << endl;
cout << lstPaises.good() << endl;
paisesE.close();
lstPaises.close();
return 0;
}
And here's the code that works with the console output:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdio.h>
#include <string>
using namespace std;
typedef char str20[21];
struct sPaises {
str20 nom,
cont;
int hab;
};
bool abrirArchivos(ifstream &archE, sPaises &rPais){
archE >> rPais.nom;
archE >> rPais.cont;
archE >> rPais.hab;
return archE.good();
}
int main(){
sPaises rPaises;
ifstream paisesE("paises.txt");
//ofstream lstPaises("ListadoPaises.txt");
while (abrirArchivos(paisesE, rPaises) == 1){
paisesE.clear();
cout << rPaises.nom << rPaises.cont << rPaises.hab << endl;
}
cout << paisesE.good() << endl;
//cout << lstPaises.good() << endl;
paisesE.close();
//lstPaises.close();
return 0;
}
As you can see they are almost the same with the difference being the paisesE.clear(); line and the ofstream related lines are commented.
A text file sample i'm trying to input:
Alemania Europa 82605000
Argentina América 43823000
Brasil América 207012000
Canadá América 36477000
China Asia 1380996000