C++ Qt cannot read the whole text file - c++

I'm writing a tool for private use. The problem is that Qt cannot read a text file containing all contents published here.
It only reads this
The three points were pasted by Qt.
My code for reading the file is following
QFile file;
file.setFileName(m_filename);
if (!file.open(QIODevice::ReadOnly))
return;
QTextStream in(&file);
while (!in.atEnd()) {
m_fileContents += in.readLine();
}
file.close();
Do you have any idea why it doesn't work?

QFile file;
file.setFileName(m_filename);
if (!file.open(QIODevice::ReadOnly))
return;
m_fileContents = file.readAll();

I just tested your code on my own computer with your data and it works well.
If you're using an IDE, maybe it does not display all the text of your final string and this is why you have three dot at the end of your sample.
Also as evilruff suggest you can use QFile::readAll method directly.

Related

How to make a QByteArray from a Database file

Using QByteArray QIODevice::readAll() from QT5, I was able to make a bytes array from a txt file or an image, used decode after and recreated the file correctly. But, when I tried with a .db file (SQLITE) it didn't work.
I noticed that when you open a .db with a text editor, you will see "SQLite format 3" followed by encoded characters. After making a QByteArray from a .db file, followed by decode() to recreate the file, when I opened it with a text editor, the file only contains the text "SQLite format 3".
Does QByteArray only work with txt file or Image file?
If it does, how can I make a Array of bytes from a .db (SQLITE) file.
Thanks
Update1 (The code belows works):
QFile file("C:/database.db");
if(!file.open(QIODevice::ReadOnly))
qDebug()<<"You are stupid!";
QByteArray byteArray = file.readAll();
QFile file2("C:/database2.db");
file2.open(QIODevice::WriteOnly);
file2.write(byteArray);
file2.close();
file.close();
Update2:
About the decode I mentioned in my initial question, I was using the following:
QString QFile::decodeName(const QByteArray & localFileName)
which make no sense when you read carefully the documentation and was just wrong. :)
You should not open that file with QIODevice::Text flag.
Check this http://doc.qt.io/qt-5/qiodevice.html#OpenModeFlag-enum

evaluateJavaScript used for an entire file

I need an editor of mine to evaluate the JS code in whatever JS file I have open. However, it never does. Although the app output says
QIODevice::read: device not open
Here's my code (mFilename is the variable that holds the open file's filename)
QFile sFile(mFilename);
QTextStream in(&sFile);
text = in.readAll();
sFile.close();
ui->webView->page()->mainFrame()->evaluateJavaScript(text);
You forgot to open the file. To open it you have to use QFile::open method.
If you want to read it just call the file.open like that:
sFile.open(QIODevice::ReadOnly);
Indeed you didn't open the file by calling the QFile constructor.
So, your code would now be:
QFile sFile(mFilename);
QTextStream in(&sFile);
sFile.open(QIODevice::ReadOnly);
text = in.readAll();
sFile.close();
ui->webView->page()->mainFrame()->evaluateJavaScript(text);

How to read unicode from a file and display the data in a QTextEdit?

I want to read unicode from file and display the corresponding data in a QTextEdit.Please give me some suggestions.
Your question is a bit poor, but you need to use QFile and QTextEdit for this as follows:
QFile file("in.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
while (!in.atEnd())
myTextEdit.append(in.readLine());
or if you are not dealing with a huge file and small memory, you can read the file in as a whole without reading lines and chunks:
QFile file("in.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
myTextEdit.setText(file.readAll());
// or setPlainText(file.readAll());
These will read the data in as unicode by default based on the documentation.
There are several ways of doing it, so this answer is just giving you some taste, and you will need to fine-tune this based on your specific scenario. You will need to add proper error handling, includes, build system files, etc.

QFile does not open file

QLabel* codeLabel = new Qlabel;
QFile file("C:\index.txt");
file.open(stderr, QIODevice::WriteOnly);
QByteArray data;
data = file.readAll();
codeLabel->setText("test"+QString(data));
file.close();
Then there is only "test" in QLabel.
Help, Please
Aside from the fact you should escape backslashes within C-style strings (c:\\index.txt), you have a problem with the following sequence:
// vvvvvvvvv
file.open(stderr, QIODevice::WriteOnly);
:
data = file.readAll();
// ^^^^
What exactly did you think was going to happen when you opened the file write-only, then tried to read it? You need to open it for reading such as with QIODevice::ReadOnly or QIODevice::ReadWrite.
On top of that, you should check the return code of all functions that fail by giving you a return code. You currently have no idea whether the file.open() worked or not.
I'm also not convinced that you should be opening stderr (which is really an ouput "device") for input. You'll almost certainly never get any actual data coming in on that file descriptor, which is probably why your input is empty.
You need to step back and ask what you're trying to acheive. For example, are you trying to capture everything your process sends to standard error? If so, it's not going to work that way.
If you're just trying to read the index.txt file, you're using the wrong overload. Remove the stderr parameter altogether:
file.open (QIODevice::ReadOnly);
If it's something else you're trying to do, add that to the question.
file.open(stderr, QIODevice::WriteOnly);
this closes the file again and reopens with the stderr stream in write only mode
you'll want to change that to
file.open(QIODevice::ReadOnly);
QFile file("C:\index.txt");
Here you try to open a file called: C:index.txt because '\i' is converted to i. You want to double you backslash:
QFile file("C:\\index.txt");
Because you read from a file you opened write-only.

How to convert ANSI format file to Unicode

I have one file that is encoded in ANSI format (showing in Notepad++ as Encoded in ANSI) and it also shows the special characters (degree celcius,pound etc.) and while reading i want to convert all the characters to unicode.
How can i convert ANSI to Unicode in C/C++ or Qt ?
My Qt is still very rusty, but something along the following lines:
QFile inFile("foo.txt");
if (!inFile.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QFile outFile("foo.out.txt");
if (!outFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
return;
QTextStream in(&inFile);
QTextStream out(&outFile);
out.setCodec("UTF-8");
while (!in.atEnd()) {
QString line = in.readLine();
out << line;
}
Pieced together from the documentation of QFile and QTextStream, both of which include examples for reading and writing files. The default for QTextStream is to use the legacy encoding, so we only need to set an explicit encoding on the output QTextStream.
If the file isn't too large you could probably also use
out << in.readAll();
instead of the loop over the lines. The loop especially might add a trailing line break to the output file (although the docs aren't very clear on that).
Hope this helps.
http://geekswithblogs.net/dastblog/archive/2006/11/24/98746.aspx
Just read it with QTextStream. It will apply QTextCodec::codecForLocale, which uses the default ("ANSI") translation of 8 bits characters to Unicode.
Note that this won't work if you've copied an ANSI text file to Mac or Linux, as they don't have the notion of ANSI. For them, the ANSI text file will be ASCII-like so you should first convert to Unicode (UTF-8) and then copy.