Skip to content

Instantly share code, notes, and snippets.

@alexsandro-xpt
Created May 23, 2012 19:09
Show Gist options
  • Save alexsandro-xpt/2777148 to your computer and use it in GitHub Desktop.
Save alexsandro-xpt/2777148 to your computer and use it in GitHub Desktop.
Trabalhando com arquivos usando C++
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <iostream.h>
void main() {
FILE *arqF = fopen("c:\\funcionarios.txt","r");
char nome[40];
int chapa;
float salario;
if(arqF == NULL) {
printf("Não pude abrir o arquivo funcionarios.txt");
getch();
exit(1);
}
printf("Dados dos funcionarios\nChapa\tNome\tSalario\n");
float salmaior=0, mediasalario=0;
int cont=0;
char nomemaior[40];
while( ! feof(arqF) ) {
cont++;
fscanf(arqF, "%d%s%f", &chapa, nome, &salario);
if (salario>salmaior){
salmaior=salario;
strcpy(nomemaior,nome);
//nomemaior=nome;
}
mediasalario=mediasalario+salario;
printf("%d\t%s\t%.2f\n", chapa, nome, salario);
}
mediasalario=mediasalario/cont;
cout<<salmaior<< " " <<mediasalario<< " "<<nomemaior;
fclose(arqF);
getch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment