Foros del Web » Programación para mayores de 30 ;) » C/C++ »

Problema al crear archivos de texto en c++

Estas en el tema de Problema al crear archivos de texto en c++ en el foro de C/C++ en Foros del Web. muchachos les dejo esto por aca.. @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código C++: Ver original #include <iostream> #include <fstream> #include <cstdlib>   using namespace std ;   void ...
  #1 (permalink)  
Antiguo 19/03/2014, 12:37
 
Fecha de Ingreso: marzo-2014
Ubicación: Buenos Aires
Mensajes: 59
Antigüedad: 10 años, 1 mes
Puntos: 0
Problema al crear archivos de texto en c++

muchachos les dejo esto por aca..
Código C++:
Ver original
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. void print_menu(int &v1, string &p1,bool &opt1, bool &opt2, bool &opt3, bool &opt4, bool &opt5, bool &opt6);
  8. void salidaTam(string p1, int salida);
  9. void salidaPer(string p1);
  10. void salidaFor(string p1);
  11. void salidaVir(string p1);
  12. void salidaCol(string p1);
  13. void sub_menu(int &v1,bool &opt1, bool &opt2, bool &opt3, bool &opt4, bool &opt5, bool &opt6);
  14.  
  15. int main()
  16. {
  17.     struct regtxt{
  18.         string tamanio;
  19.         string perforaciones;
  20.         string forma;
  21.         string virola;
  22.         string color;
  23.     };
  24.    
  25.     string subopc;
  26.     int resp;
  27.     int resp2;
  28.     int resp3;
  29.     bool opc1,opc2,opc3,opc4,opc5,opc6;
  30.     opc1=opc2=opc3=opc4=opc5=opc6=true;
  31.     regtxt registro;
  32.  
  33.     print_menu(resp,subopc,opc1,opc2,opc3,opc4,opc5,opc6);
  34.     while(resp != 6){
  35.         print_menu(resp,subopc,opc1,opc2,opc3,opc4,opc5,opc6);
  36.     }
  37.     return 0;
  38. }
  39.  
  40.  
  41. void salidaTam(string p1, int salida){
  42.     string linea;
  43.     ifstream fentrada("Botones.txt");
  44.     ofstream fsalida1("Salida1.txt");
  45.     while(!fentrada.eof()){
  46.         getline(fentrada,linea);
  47.             if (p1=="a"){
  48.                 if (linea.substr(0,1)=="c"){
  49.                     fsalida1<<linea<<endl;
  50.                 }
  51.             }
  52.             if (p1=="b"){
  53.                 if (linea.substr(0,1)=="m"){
  54.                     fsalida1<<linea<<endl;
  55.                 }
  56.             }
  57.             if (p1=="c"){
  58.                 if (linea.substr(0,1)=="g"){
  59.                     fsalida1<<linea<<endl;
  60.                 }
  61.             }
  62.     }
  63. }
  64.  
  65. void salidaPer(string p1){
  66.     string linea;
  67.     ifstream fentrada("Botones.txt");
  68.     ofstream fsalida1("Salida1.txt");
  69.     while(!fentrada.eof()){
  70.         getline(fentrada,linea);
  71.             if (p1=="a"){
  72.                 if (linea.substr(2,1)=="1"){
  73.                     fsalida1<<linea<<endl;
  74.                 }
  75.             }
  76.             if (p1=="b"){
  77.                 if (linea.substr(2,1)=="2"){
  78.                     fsalida1<<linea<<endl;
  79.                 }
  80.             }
  81.             if (p1=="c"){
  82.                 if (linea.substr(2,1)=="4"){
  83.                     fsalida1<<linea<<endl;
  84.                 }
  85.             }
  86.     }
  87. }
  88.  
  89. void salidaFor(string p1){
  90.     string linea;
  91.     ifstream fentrada("Botones.txt");
  92.     ofstream fsalida1("Salida1.txt");
  93.     while(!fentrada.eof()){
  94.         getline(fentrada,linea);
  95.             if (p1=="a"){
  96.                 if (linea.substr(4,4)=="Redo"){
  97.                     fsalida1<<linea<<endl;
  98.                 }
  99.             }
  100.             if (p1=="b"){
  101.                 if (linea.substr(4,4)=="Cuad"){
  102.                     fsalida1<<linea<<endl;
  103.                 }
  104.             }
  105.             if (p1=="c"){
  106.                 if (linea.substr(4,4)=="Tria"){
  107.                     fsalida1<<linea<<endl;
  108.                 }
  109.             }
  110.     }
  111. }
  112.  
  113. void salidaVir(string p1){
  114.     string linea;
  115.     ifstream fentrada("Botones.txt");
  116.     ofstream fsalida1("Salida1.txt");
  117.     while(!fentrada.eof()){
  118.         getline(fentrada,linea);
  119.             if (p1=="a"){
  120.                 if (linea.substr(9,3)=="Con"){
  121.                     fsalida1<<linea<<endl;
  122.                 }
  123.             }
  124.             if (p1=="b"){
  125.                 if (linea.substr(9,3)=="Sin"){
  126.                     fsalida1<<linea<<endl;
  127.                 }
  128.             }
  129.     }
  130. }
  131.  
  132. void salidaCol(string p1){
  133.     string linea;
  134.     ifstream fentrada("Botones.txt");
  135.     ofstream fsalida1("Salida1.txt");
  136.     while(!fentrada.eof()){
  137.         getline(fentrada,linea);
  138.             if (p1=="a"){
  139.                 if (linea.substr(13,5)=="Blanc"){
  140.                     fsalida1<<linea<<endl;
  141.                 }
  142.             }
  143.             if (p1=="b"){
  144.                 if (linea.substr(13,5)=="Negro"){
  145.                     fsalida1<<linea<<endl;
  146.                 }
  147.             }
  148.             if (p1=="c"){
  149.                 if (linea.substr(13,5)=="Rojo "){
  150.                     fsalida1<<linea<<endl;
  151.                 }
  152.             }
  153.             if (p1=="d"){
  154.                 if (linea.substr(13,5)=="Amari"){
  155.                     fsalida1<<linea<<endl;
  156.                 }
  157.             }
  158.     }
  159. }
  160.  
  161. void sub_menu(int &v1,bool &opt1, bool &opt2, bool &opt3, bool &opt4, bool &opt5, bool &opt6, int &salida){
  162.  
  163.     int v2;
  164.     do{
  165.         cout << "Elija como quiere seguir: " <<endl;
  166.         cout << "1. Seguir aplicando filtros a la caja actual" <<endl;
  167.         cout << "2. Crear otra caja" <<endl;
  168.         cout << "3. Salir del programa" <<endl;
  169.         cin>>v2;
  170.     }while(v2 <= 0 || v2 >3);
  171.  
  172.     if(v2==1){
  173.         return;
  174.     }
  175.     if(v2==2){
  176.         opt1=opt2=opt3=opt4=opt5=opt6=true;
  177.         salida++;
  178.     }
  179.     if(v2==3){
  180.         v1 = 6;
  181.     }
  182. }
  183.  
  184.  
  185. void print_menu(int &v1, string &p1,bool &opt1, bool &opt2, bool &opt3, bool &opt4, bool &opt5, bool &opt6){
  186.  
  187.     int resp=0,cond1=0,cond2=0,cond3=0,cond4=0,cond5=0;
  188.     int salida=1; //variable que define el nombre de salida (todavia no se como aplicarla)
  189.     string subopc;
  190.     do{
  191.         cout << "Elija como quiere filtrar los botones: " <<endl;
  192.         if (opt1){
  193.             cout << "1. Tamanio" <<endl;
  194.         }
  195.         if (opt2){
  196.             cout << "2. Perforaciones" <<endl;
  197.         }
  198.         if (opt3){
  199.             cout << "3. Forma" <<endl;
  200.         }
  201.         if (opt4){
  202.             cout << "4. Virola" <<endl;
  203.         }
  204.         if (opt5){
  205.             cout << "5. Color" <<endl;
  206.         }
  207.         if (opt6){
  208.             cout << "6. Salir del programa" <<endl;
  209.         }
  210.  
  211.         cin>> v1;
  212.  
  213.         if(v1 == 1 && opt1 != false){
  214.             opt1 = false;
  215.             do{
  216.                 cout << "Eliga la subopcion" <<endl;
  217.                 cout << "a. Chico" << endl;
  218.                 cout << "b. Mediano" << endl;
  219.                 cout << "c. Grande" << endl;
  220.                 cin >> p1;
  221.             }while (p1 < "a" || p1 > "c");
  222.             salidaTam(p1,salida);
  223.         }
  224.         if (v1 == 2 && opt2 != false){
  225.             opt2 = false;
  226.             do{
  227.                 cout << "Eliga la subopcion" <<endl;
  228.                 cout << "a. 1 Perforacion" << endl;
  229.                 cout << "b. 2 Perforaciones" << endl;
  230.                 cout << "c. 4 Perforaciones" << endl;
  231.                 cin >> p1;
  232.             }while (p1 < "a" || p1 > "c");
  233.             salidaPer(p1);
  234.         }
  235.         if (v1 == 3 && opt3 != false){
  236.             opt3 = false;
  237.             do{
  238.                 cout << "Eliga la subopcion" <<endl;
  239.                 cout << "a. Redondo" << endl;
  240.                 cout << "b. Cuadrado" << endl;
  241.                 cout << "c. Triangulo" << endl;
  242.                 cin >> p1;
  243.             }while (p1 < "a" || p1 > "c");
  244.             salidaFor(p1);
  245.         }
  246.         if(v1 == 4 && opt4 != false){
  247.             opt4 = false;
  248.             do{
  249.                 cout << "Eliga la subopcion" <<endl;
  250.                 cout << "a. Con virola" << endl;
  251.                 cout << "b. Sin virola" << endl;
  252.                 cin >> p1;
  253.             }while (p1 < "a" || p1 > "b");
  254.             salidaVir(p1);
  255.         }
  256.         if(v1 == 5 && opt5 != false){
  257.             opt5 = false;
  258.             do{
  259.                 cout << "Eliga la subopcion" <<endl;
  260.                 cout << "a. Blanco" << endl;
  261.                 cout << "b. Negro" << endl;
  262.                 cout << "c. Rojo" << endl;
  263.                 cout << "d. Amarillo" << endl;
  264.                 cin >> p1;
  265.             }while (p1 < "a" || p1 > "d");
  266.             salidaCol(p1);
  267.         }
  268.         sub_menu(v1,opt1,opt2,opt3,opt4,opt5,opt6,salida);
  269.     }while(v1 <= 0 || v1 >6);
  270.  
  271. }


Código C++:
Ver original
  1. void salidaTam(string p1, int salida){
  2.     string linea;
  3.     ifstream fentrada("Botones.txt");
  4.     ofstream fsalida1("Salida1.txt");
  5.     while(!fentrada.eof()){
  6.         getline(fentrada,linea);
  7.             if (p1=="a"){
  8.                 if (linea.substr(0,1)=="c"){
  9.                     fsalida1<<linea<<endl;
  10.                 }
  11.             }
  12.             if (p1=="b"){
  13.                 if (linea.substr(0,1)=="m"){
  14.                     fsalida1<<linea<<endl;
  15.                 }
  16.             }
  17.             if (p1=="c"){
  18.                 if (linea.substr(0,1)=="g"){
  19.                     fsalida1<<linea<<endl;
  20.                 }
  21.             }
  22.     }
  23. }


Lo que yo quiero hacer es que cuando se decida crear otra caja en mi programa principal, que se cree un nuevo archivo con distinto nombre pero no se como hacer
les deje el codigo completo por las dudas pero no tengo idea, probe muchas cosas ya :/
  #2 (permalink)  
Antiguo 19/03/2014, 12:38
 
Fecha de Ingreso: marzo-2014
Ubicación: Buenos Aires
Mensajes: 59
Antigüedad: 10 años, 1 mes
Puntos: 0
Respuesta: Problema al crear archivos de texto en c++

Osea mas simple es se puede guardar un archivo con un nombre que cambie en base a una variable ?
  #3 (permalink)  
Antiguo 19/03/2014, 12:45
 
Fecha de Ingreso: enero-2012
Ubicación: Buenos Aires
Mensajes: 745
Antigüedad: 12 años, 3 meses
Puntos: 35
Respuesta: Problema al crear archivos de texto en c++

Así?

Código C++:
Ver original
  1. void salidaTam(string p1, int salida, string nombre_archivo)

Y luego:

Código C++:
Ver original
  1. ofstream fsalida1(nombre_archivo);

Y para llamar a la funcion:

Código C++:
Ver original
  1. salidaTam(p1, salida, "archivo1.txt");

  #4 (permalink)  
Antiguo 19/03/2014, 13:34
 
Fecha de Ingreso: marzo-2014
Ubicación: Buenos Aires
Mensajes: 59
Antigüedad: 10 años, 1 mes
Puntos: 0
Respuesta: Problema al crear archivos de texto en c++

eso ya lo probé, pero el ofstream no me deja pasarle un string como parámetro no se porque :/ , lo que yo quiero es que cuando salida valga 2 que quede
Código C++:
Ver original
  1. ofstream fsalida1("salida2.txt" )
por ejemplo
  #5 (permalink)  
Antiguo 19/03/2014, 15:46
 
Fecha de Ingreso: enero-2012
Ubicación: Buenos Aires
Mensajes: 745
Antigüedad: 12 años, 3 meses
Puntos: 35
Respuesta: Problema al crear archivos de texto en c++

Si eso es lo que quieres puedes hacer:

Código C++:
Ver original
  1. ofstream fsalida1;
  2. string filename;
  3.  
  4. if (salida == 2)
  5.     filename = "salida2.txt";
  6. else
  7.     filename = "salida1.txt";
  8.  
  9. fsalida1.open(filename);

No lo probé el código pero debería funcionar

  #6 (permalink)  
Antiguo 26/03/2014, 20:03
 
Fecha de Ingreso: marzo-2014
Ubicación: Buenos Aires
Mensajes: 59
Antigüedad: 10 años, 1 mes
Puntos: 0
Respuesta: Problema al crear archivos de texto en c++

Claro lo que yo quiero hacer es algo parecido a eso,1ro pero al ser un ciclo while no se cuantas veces le voy a cambiar el nombre a filename y 2do aca

fsalida1.open(filename);

no me deja pasarle filename, como parametro :/

Etiquetas: int, programa, string
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 15:07.