Está en la página 1de 2

ENTRADA/SALIDA DE ARCHIVOS cout << "REGISTRO.........

\n";
cout << "ID: " << id << endl;
#include <iostream> cout << "NOMBRE: " << nombre << endl;
#include <fstream> cout << "EDAD: " << edad << endl;
#include <cstdlib> cout << "ESTATURA: " << est << endl;
}
using namespace std; archivo.close();
}
fstream archivo; //incluye ifstream y ofstream else
void leerArchivo1(){ {
archivo.open("datos.txt",ios::in); cout << "No se encontro el archivo";
if(archivo.is_open()) { }
string linea;
while(getline(archivo,linea)) }
{
cout << linea << endl;
} int main()
archivo.close(); {
}
else leerArchivo3();
{ system("pause");
cout << "Lo siento no se encontro el return 0;
archivo"; }
}
SUMA DE NUMEROS PRIMOS
}
#include <iostream>
void leerArchivo2(){ #include <math.h>
archivo.open("datos.txt",ios::in); #include <stdlib.h>
if(archivo.is_open()) {
char c; using namespace std;
while(!archivo.eof())
{ int main(int argc, char *argv[])
c = archivo.get(); {int n=0,c=0,c2=0,res=0,nc=0, suma=0, total = 0;
cout <<c; cout<<"Hasta que numero quieres encontrar los
} numeros primos: ";
archivo.close(); cin>>n;
} for(c=1;c<=n;c++)
else {
{ for(c2=1;c2<=c;c2++)
cout << "No se encontro el archivo"; {
} res=c%c2;
if(res==0)
} {
nc=nc+1;
void leerArchivo3(){ }
archivo.open("datos.txt",ios::in); }
if(archivo.is_open()) { if(nc==2)
int id; {
char nombre [50]; cout<<" "<<c;
int edad; total=total+c;
float est; }
while(!archivo.eof()){ nc=0;
archivo>>id>>nombre>>edad>>est;
}
cout <<"\nLa suma de los numeros primos es: "
<<total <<endl;
system("pause");
return 0;

PROMEDIOS

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

fstream archivo; //incluye ifstream y ofstream

void leerArchivo3(){
archivo.open("datos.txt",ios::in);
if(archivo.is_open()) {
int id;
char nombre [50];
float prom;
while(!archivo.eof()){
archivo>>id>>nombre>>prom;
cout << "REGISTRO.........\n";
cout << "ID: " << id << endl;
cout << "NOMBRE: " << nombre << endl;
cout << "PROMEDIO: " << prom << endl;
}
archivo.close();
}
else
{
cout << "No se encontro el archivo";
}

int main()
{

leerArchivo3();
system("pause");
return 0;
}

También podría gustarte