la lectura de los datos facilitados un nombre de archivo (C + +)

  • Ponzzz
  • Born
  • Born
  • No Avatar
  • Registrado: Abr 02, 2010
  • Mensajes: 3
  • Status: Offline

Nota Abril 4th, 2010, 11:04 pm

Estoy tratando de implementar una función llamada AddFile (archivo de cadenas). Esto hace exactamente como suena, se toman los datos de un segundo archivo y leerlo pulg Cuando me encuentro con problemas es cómo hacer para guardar el nombre del archivo por lo que el siguiente código funciona.

Código: [ Select ]
ifstream myfile (file);


esto generará un error ya que el argumento no tiene una cadena (por lo que sé su un const char * <name>). Tengo que dar el nombre del archivo y guardarlo en una variable por lo que no es tan fácil como simplemente el nombre del archivo suministrar directamente en el código. No puedo encontrar la manera de suministrar el argumento con el tipo de variable adecuado y también su aplicación como el siguiente código. Tal vez soy sólo overthinking este lol

Código: [ Select ]
cout << "What is the name of the file you would like to add?" << endl;
        cin >> input;

        addFile(input);
  1. cout << "What is the name of the file you would like to add?" << endl;
  2.         cin >> input;
  3.         addFile(input);


cualquier consejo o sugerencia?
  • Anonymous
  • Bot
  • No Avatar
  • Registrado: 25 Feb 2008
  • Mensajes: ?
  • Loc: Ozzuland
  • Status: Online

Nota Abril 4th, 2010, 11:04 pm

  • spork
  • Brewmaster
  • Silver Member
  • Avatar de Usuario
  • Registrado: Sep 22, 2003
  • Mensajes: 6130
  • Loc: Seattle, WA
  • Status: Offline

Nota Abril 5th, 2010, 8:06 am

Guarde el archivo en un std:: string, y luego pase el c_string subyacentes a la función:

CPP Código: [ Select ]
string input;
cout << "What is the name of the file you would like to add?"
     << endl;
cin >> input;
if( input != "" ) {
    addFile(input.c_str());
}
  1. string input;
  2. cout << "What is the name of the file you would like to add?"
  3.      << endl;
  4. cin >> input;
  5. if( input != "" ) {
  6.     addFile(input.c_str());
  7. }
The Beer Monocle. Classy.

Publicar Información

  • Total de mensajes en este tema: 2 mensajes
  • Usuarios navegando por este Foro: Bigwebmaster y 123 invitados
  • No puede abrir nuevos temas en este Foro
  • No puede responder a temas en este Foro
  • No puede editar sus mensajes en este Foro
  • No puede borrar sus mensajes en este Foro
  • No puede enviar adjuntos en este Foro
 
 

© 2011 Unmelted, LLC. Ozzu® es una marca registrada de Unmelted, LLC