Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: novatus84 en 2 Diciembre 2014, 19:17 pm



Título: Ayuda tolower & overwrite c++
Publicado por: novatus84 en 2 Diciembre 2014, 19:17 pm
Buenas tengo casi terminado el programa aunque me falta rematarlo. Necesito que el usuario decida por teclado si quiere sobrescribir el data2.txt que ya existe creado o no , esta parte la tengo casi casi. Por otra parte no consigo hacer funcionar la función tolower en un vector string ahi tengo varias pruebas que no consigo que funcionen muchas gracias de antemano.
Código
  1. #include<string>    // std::string
  2. #include<vector>    // std::vector<>
  3. #include<iostream>  // std::cout | std::cin | std::cerr
  4. #include<fstream>   // std::ifstream | std::ofstream
  5. #include<cstdlib>   // std::exit()
  6. #include<algorithm>  // std:: sort()
  7. #include <locale>  // std::tolower | st::locale
  8. #include <cctype>   //tolower
  9.  
  10. using namespace std;
  11.  
  12. vector<string> load_vector(const string&);
  13. void save_vector(const vector<string>&, const string&);
  14. void error(const string&);
  15.  
  16. int main() {
  17.  
  18.    string filename = "data.txt";
  19.    vector<string> text = load_vector(filename);
  20.  
  21.    string filename2 = "data2.txt";
  22.  
  23.    fstream file;    // start to see if we want to overwrite the file if the file already exist
  24.    file.open("data2.txt", ios_base::out | ios_base::in);  // will not create file
  25.    if (file.is_open())
  26.    {
  27.        cout << "Warning, file already exists, proceed?";
  28.        if (!file.is_open())
  29.        {
  30.            file.close();
  31.  
  32.        }
  33.    }
  34.    else
  35.    {
  36.        file.clear();
  37.        file.open("data2.txt", ios_base::out);  // will create if necessary
  38.    }
  39.  
  40.  
  41.  
  42.    sort(text.begin(), text.end());
  43.    text.erase(unique(text.begin(), text.end()), text.end());
  44.  
  45.  
  46.    /*for (unsigned int i=0; i<text.size(); i++) {
  47.       text[i] = tolower ();
  48.     }
  49.    
  50.    // for (auto word : text) {
  51.      //   string lowcase;
  52.        // for (auto ch : word)
  53.          //   if (ch>='A' and ch<='Z') lowcase += ch +'a'-'A' ; else lowcase = ch;
  54.        // word = lowcase;
  55.   } */
  56.  
  57.    save_vector(text, filename2);
  58.    return 0;
  59.  
  60.  
  61.  
  62. }
  63.  
  64. vector<string> load_vector(const string& filename) {
  65.    vector<string> vi;
  66.  
  67.    ifstream ifile {filename};
  68.    if (!ifile)
  69.        error("load_vector cannot open the file " + filename);
  70.    copy(istream_iterator<string>(ifile), istream_iterator<string>(), back_inserter(vi));
  71.  
  72.    return vi;
  73. }
  74.  
  75.  
  76. void save_vector(const vector<string>& vi, const string& filename2) {
  77.  
  78.    ofstream ofile ("data2.txt", ofstream::out);
  79.    if (!ofile)
  80.        error("save_vector cannot open the file " + filename2);
  81.    for (auto n : vi)
  82.        ofile << n << ' ';
  83.    ofile.close();
  84.    cout << "\nVector written in file \"" << filename2 << "\"" << endl;
  85. }
  86. /* return true if two vector<string> contain exactly the same members
  87. bool compare(const vector<string>& v1, const vector<string>& v2) {
  88.     if (v1.size()!=v2.size())
  89.         return false;       // vectors have different sizes
  90.    
  91.     for (int i=0; i<v1.size(); ++i)
  92.         if (v1[i]!=v2[i])
  93.             return false;   // i-th members of v1 and v2 are different
  94.    
  95.     return true;
  96. }
  97. */
  98. void error(const string& s) {
  99.    cerr << "Error: " << s << endl;
  100.    exit(EXIT_FAILURE);
  101. }


Título: Re: Ayuda tolower & overwrite c++
Publicado por: avesudra en 3 Diciembre 2014, 11:56 am
Lo de la función tolower lo puedes solucionar con la función transform de la biblioteca <algorithm> , que te permite hacer una operación específica sobre cada elemento que recorre un iterador.

Código
  1. std::string str = "mi cadena";
  2. std::transform(str.begin(), str.end(), str.begin(), std::tolower);

Un saludo.