Are you deliberately trying to confuse?
From your first post, you have a text file containing ASCII text:
ABCD
AAAA
BBBB
ABCD
AAAA
BBBB
To copy to clipboard, switch view to plain text mode
you wish to transform into a text file containing more ASCII text:
65 66 67 68
65 65 65 65
66 66 66 66
65 66 67 68
65 65 65 65
66 66 66 66
To copy to clipboard, switch view to plain text mode
Your first batch of code does some sort of mangling of a single value based on the three values streamed from the file and doesn't attempt to output anything to a file. In you last post you are renaming an open file, checking whether it exists, all to do something with a password?
Your original problem is trivially met with:
QFile input
("input.txt");
QFile output
("output.txt");
bool needSpace = false;
char c;
while (input.getChar(&c)) {
if (c < ' ') { // new line and other control chars
output.putChar(c);
needSpace = false;
}
else {
if (needSpace)
output.putChar(' ');
needSpace = true;
}
}
}
}
QFile input("input.txt");
QFile output("output.txt");
if (input.open(QIODevice::ReadOnly)) {
if (output.open(QIODevice::WriteOnly)) {
bool needSpace = false;
char c;
while (input.getChar(&c)) {
if (c < ' ') { // new line and other control chars
output.putChar(c);
needSpace = false;
}
else {
if (needSpace)
output.putChar(' ');
output.write( QByteArray::number(c) );
needSpace = true;
}
}
}
}
To copy to clipboard, switch view to plain text mode
or, using QTextStream:
char c;
bool needSpace = false;
while (!in.atEnd()) {
in >> c;
if (c < ' ') {
out << c;
needSpace = false;
}
else {
out <<
(needSpace?
" ": "") <<
QByteArray::number(c
);
needSpace = true;
}
}
}
}
if (input.open(QIODevice::ReadOnly)) {
if (output.open(QIODevice::WriteOnly)) {
QTextStream in(&input);
QTextStream out(&output);
char c;
bool needSpace = false;
while (!in.atEnd()) {
in >> c;
if (c < ' ') {
out << c;
needSpace = false;
}
else {
out << (needSpace? " ": "") << QByteArray::number(c);
needSpace = true;
}
}
}
}
To copy to clipboard, switch view to plain text mode
There are many ways to achieve this.
Bookmarks