Skip to content

Instantly share code, notes, and snippets.

@rbarros
Last active August 29, 2015 14:13
Show Gist options
  • Save rbarros/70741d0087b95dd66574 to your computer and use it in GitHub Desktop.
Save rbarros/70741d0087b95dd66574 to your computer and use it in GitHub Desktop.
Exemplos em C/C++
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
void main() {
char aux[256], *words[50], *pTmp;
int c, i;
// Lê string
gets(aux);
// Separa em palavras
for(c = 0, words[c] = strtok(aux, " \t"); words[c]; words[++c] = strtok(0, " \t"));
// Exibe palavras
printf("Palavras: %d", c);
for(i = 0; i < c; i++)
printf("\n%s", words[i]);
}
// Programa para ler os dados do arquivo "matricula.dat" e apresentá-los na tela.
#include<iostream>
#include<fstream>
using namespace std;
using std::ifstream;
int main() {
ifstream arqmat;
int matricula; // numero de matricula do aluno
// Abrindo arquivo binario para leitura
arqmat.open("matricula.dat", ios::binary);
// Lendo matriculas do alunos do arquivo e apresentando-as na tela
count << "Matriculas:\n";
arqmat.read((char *)(&matricula), sizeof(int));
while(!arqmat.eof()) {
count << matricula << endl;
arqmat.read((char *)(&matricula), sizeof(int));
}
arqmat.close();
return 0;
}
// Programa para ler várias strings e as armazene em um arquivo texto.
#include<iostream>
#include<fstream>
using namespace std;
using std::ofstream;
int main() {
char filename[100];
ofstream file;
char msg[256];
// Lendo nome do arquivo de saída
count << "Digite o nome do arquivo a ser gerado: ";
cin.getline(filname, 100);
// Abrindo arquivo para escrita
arquivo.open(filename);
// Armazena dados
count << "Digite o texto a ser armazenado (-1 para terminar):\n";
cin.getline(msg, 256);
while(strcmp(msg, "-1") != 0) {
file << msg << endl;
cin.getline(msg, 256);
}
// Fechando arquivo
filename.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment