Este es un ejemplo básico sobre como utilizar la clase QTextStream para escribir texto a un archivo, y tambien, para leerlo. (Archivos de texto)
#include <QFile>
#include <QTextStream>
#include <QDebug>
int main(int argc, char *argv[])
{
//to avoid warnings.
Q_UNUSED(argc);
Q_UNUSED(argv);
QString text;
//Open the file for writing, and set the text stream to write, to the file.
QFile file_for_writing("file.txt");
file_for_writing.open(QIODevice::WriteOnly | QIODevice::Truncate); //
QTextStream text_stream_for_writing(&file_for_writing);
//set the text of the file
text = "My son ask for thyself another Kingdom, for that which I leave is too small for thee - King Philip of Macedonia";
//Write the text on the stream
text_stream_for_writing << text;
//close the file
file_for_writing.close();
//empty the text.
text.clear();
//Open the file for reading and set the text stream to read, from the file.
QFile file_for_reading("file.txt");
file_for_reading.open(QIODevice::ReadOnly);
QTextStream text_stream_for_reading(&file_for_reading);
//read 6 characters
qDebug() << text_stream_for_reading.read(6);
//Reset the position on the file
text_stream_for_reading.seek(0);
//read a word
text_stream_for_reading >> text;
qDebug() << text;
//Reset the position on the file
text_stream_for_reading.seek(0);
//read a line
qDebug() << text_stream_for_reading.readLine();
//Reset the position on the file
text_stream_for_reading.seek(0);
//read the entire content of the file
qDebug() << text_stream_for_reading.readAll();
//close the file
file_for_reading.close();
}



7 comments
1 ping
Tete Oliva
April 13, 2011 at 10:31 pm (UTC -6) Link to this comment
MUY BUEN APORTE!!!! ME SIRVIO DE MUCHO
GRACIAS!!!!
Lex
April 14, 2011 at 1:26 pm (UTC -6) Link to this comment
Muchas gracias, me alegro que te haya servido.
Ronny
May 27, 2011 at 3:43 pm (UTC -6) Link to this comment
Los parametros de entrada argc y argv a que corresponden?
Lex
May 30, 2011 at 9:33 am (UTC -6) Link to this comment
Esos son nombres dados clasicamente a los parametros de entrada para el programa. Es decir, los utilizados en la linea de comando.
Por ejemplo, cuando se ejecuta:
nombre_aplicacion parametro1 parametro2
argc contendra el numero de parametros, para este ejemplo sera 3; y argv es un arreglo de char* que contendra cada uno de los parametros: [nombre_aplicacion, parametro1, parametro2]. Si incluye el nombre de la aplicacion como uno de los parametros
Esto no es algo incluido por el framework Qt, ni menos de C++, sino que viene desde C.
El compilador, al evaluar estos dos parametros en la funcion main arroja dos mensajes (no de error) que indican que los mismos no estan siendo utilizados (repito, esto no es un error). La idea detras de cada funcion/metodo es que los parametros declarados se utilicen, por razones obvias.
Para evitar estos mensajes, use el macro incluido en Qt: Q_UNUSED(parametro); el cual le indica al compilador que, de hecho, los parametros no son utilizados y no debe arrojar mensajes de error.
Lex
May 30, 2011 at 9:39 am (UTC -6) Link to this comment
Se me olvido mencionar, que estos parametros, por lo general, son utilizados en aplicaciones que correran en la linea de comandos, terminal, consola, etc.
Para aplicaciones con interfaz grafica, por lo general no se utilizan.
Un par de ejemplos:
Windows
copy c:windowssetuplog.txt c:temp (en este caso 3 parametros)
Linux
top -b -n 1 (en este caso, 4 parametros, el programa es el que debera averiguar la relacion que el parametro 1 tiene para el parametro -n)
Tratare de agregar un ejemplo funcional en el blog, en poco tiempo… y pondre el link aca.
marco
February 15, 2012 at 10:23 am (UTC -6) Link to this comment
disculpa donde se encuentra la ubicacion de file ? no esta en el mismo proyecto..
Ni pena Ni gloria
February 15, 2012 at 4:36 pm (UTC -6) Link to this comment
Disculpa, en qué línea?
Parámetros de main, argc y argv | Qt-Init
May 30, 2011 at 6:29 pm (UTC -6) Link to this comment
[...] un rato, por medio de comentarios (1 y 2) aclaré que significaban los parámetros de la función main(argc, [...]