Skip to content

Instantly share code, notes, and snippets.

@KaterineM
Last active August 29, 2015 14:23
Show Gist options
  • Save KaterineM/ced8f2985b549f40e153 to your computer and use it in GitHub Desktop.
Save KaterineM/ced8f2985b549f40e153 to your computer and use it in GitHub Desktop.
This program creates a new file with the intersection of the words of two input files.
/*
Copyright (C) 2015 Katerine Muñoz Tello <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*** Nombre: Katerine Muñoz Tello.
*** Objetivo: Genere un nuevo archivo que contenga la intersección de palabras de ambos archivos.
*** SO: Ubuntu 15.04
*** IDE: Sublime text 3.
*** Compilador: gcc version 4.9.2 (Ubuntu 4.9.2-10ubuntu13).
*/
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
void crearArchivoSalida(string palabra, ifstream &file); // Crea el archivo con las intersecciones.
int Iguales(string palabra, ifstream &file); // Verifica las palabras iguales.
int main(){
string palabra;
ifstream texto1;
ifstream texto2;
texto1.open("texto3.txt");
texto2.open("texto4.txt");
if(texto1.fail() && texto2.fail()){
cout << "El archivo no existe.";
system("PAUSE");
exit (1);
}
else{
while(texto1 >> palabra){
crearArchivoSalida(palabra, texto2);
}
}
texto1.close();
texto2.close();
return 0;
}
int Iguales(string palabra, ifstream &file){
string palabra2;
int cumple = 0;
file.clear();
file.seekg(0, ios::beg);
while(file >> palabra2){
if(palabra == palabra2){
cumple = 1;
}
}
return cumple;
}
void crearArchivoSalida(string palabra, ifstream &file){
int existe = 0;
string palabra2, p3;
fstream newfile;
newfile.open("texto_interseccion.txt", ios::in);
existe = Iguales(palabra, file);
while(newfile >> p3){
if(palabra == p3){
existe = 0;
}
}
if(existe == 1){
newfile.close();
newfile.open("texto_interseccion.txt", ios::app);
newfile << palabra << "\t";
}
newfile.close();
}
@KaterineM
Copy link
Author

Disclaimer

I know is a really bad habit for me to code in both spanish and english (as you may see, there're variables on those languages) BUT I'm coding in C++ since the begining of the year, so, excuse me please :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment