PDA

View Full Version : ASCII Number to Strings & Chars



ttimt
26th November 2012, 16:53
I search for help online for a whole day, maybe 2.
I outputted strings to its ascii value to a file by using the for loops and char .at()

all these output went well, but how can i read it back from the file?

i tried qt library and native c++ (not problem in codeblocks, but i want to have gui)

I'm not sure how to use char array below because I wont know how long the string is, how many chars are there.
And in my file, its not only 1 line of code. For example, when i input

ABCD
AAAA
BBBB

my file will become

65 66 67 68
65 65 65 65
66 66 66 66


ifstream myFile("abc.txt");

while(myFile >> a >> a1 >> a2){


a = a - 1;
a = a1 - 1;
a= a2 - 1;
QString abc = QChar( a + a1 + a2);
ui->aLine->setText(abc);
}


when i use just these codes below, it will only shows the first character of the whole strings.
And is there a way to make the abc still usable outside the loop? cause i need to set it after the loops right? and it always shows not declared.


myFile >> a
QString abc = QChar( a );
ui->aLine->setText(abc);


QFile mFile("abc.txt");
mFile.open(QIODevice::ReadOnly | QIODevice::Text);
QDataStream in(&mFile);

in >> a; // Not sure what's next. it keeps error

ChrisW67
26th November 2012, 20:50
Your first code snippnet makes no sense, and I cannot see its connection to the stated input and output.

A QChar is a single character, and a QString is a collection of them. You can access a single character from a QString using QString::at(). You can get the character's Unicode code point from QChar::unicode(). You probably do not need either QString or QChar to do this task.

QDataStream is not the correct tool to read a text file. See QTextStream

Generic C++ code that already works stills works in the presence of Qt. The use of Qt in your program does not magically make the rules of C++ disappear. Variables have the same scope in Qt-using programs as they do in any other C++ program.

ttimt
27th November 2012, 06:38
well, can u pls show the code?
my file have numbers, so i can still use qtextstream? and i'm gonna edit the file extension to some random thing.
I have integers in my file, and when i read it, i want it to convert to chars and to QString. Whenever it meets a new line, it saves it in a new QString.

And btw, what's the difference of qfile and qiodevice? they seems to have the same function. qiodevice::readonly , qfile::readonly.

thx

amleto
27th November 2012, 07:03
my file have numbers, so i can still use qtextstream?
Yes. why don't you try it...

ttimt
27th November 2012, 14:20
void reset1::on_pushButton_clicked()
{

QFile chk("configp.dll");
chk.open(QFile::ReadOnly);

if(chk.exists()){
chk.rename(QString("configp1.dll"));

QTextStream in(&chk);
QString w;
w = in.readLine().;


ui->label_3->setText(w);

}else{
ui->label->setText("Password not Found! Reset Password Fail");
ui->label_3->setText("Press cancel to go back");
}
}

my program becomes not responding. I solved it by putting 1 in readLine() and it wont show anything.
Also how do i convert these numbers to char? if i put .fromAscii() behind readline it shows "no matching function for call to 'QString::fromAscii();"

This is how i input it.


QFile config("configp.dll");
config.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&config);



for (int i = 0; i < ui->aLine->text().length(); i++)
{
char x =ui->aLine->text().toAscii().at(i);
out << int(x) << " ";
}out << endl;

ChrisW67
27th November 2012, 22:21
Are you deliberately trying to confuse?

From your first post, you have a text file containing ASCII text:


ABCD
AAAA
BBBB

you wish to transform into a text file containing more ASCII text:


65 66 67 68
65 65 65 65
66 66 66 66

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");
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;
}
}
}
}

or, using QTextStream:


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;
}
}
}
}

There are many ways to achieve this.

ttimt
28th November 2012, 04:21
absolutely not, the rename file doesn't work so i actually juz put there... :/
this is the output to file code,no problem with output, just showing the code
when i enter words on lineedit, it will be saved to numbers in configp.dll
66 66 66
67 67 67
68 68 68



QFile config("configp.dll");
config.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&config);

for (int i = 0; i < ui->aLine->text().length(); i++)
{
char x =ui->aLine->text().toAscii().at(i);
out << int(x) << " ";
}
out << endl;

for (int i = 0; i < ui->pLine->text().length(); i++)
{
char x =ui->pLine->text().toAscii().at(i);
out << int(x)<< " ";
}
out << endl;

for (int i = 0; i < ui->fLine->text().length(); i++)
{
char x =ui->fLine->text().toAscii().at(i);
out << int(x)<< " ";
}
out << endl;

for (int i = 0; i < ui->gLine->text().length(); i++)
{
char x =ui->gLine->text().toAscii().at(i);
out << int(x)<< " ";
}


so how im gonna get string x= aaa , string y = bbb ,string z = ccc which inputted earlier?



QFile chk("configp.dll");
chk.open(QIODevice::ReadOnly);
if(chk.exists()){

//what should i type here when file exists to get the above (string x, y, z)

}else{
ui->label->setText("File not Found!");
ui->label_3->setText("Press cancel to go back");
}

ChrisW67
28th November 2012, 06:28
Right. So, is this the question you wanted to ask?

I have a file containing three lines like:


65 65 65 66
66 66 67
67 68 69

where each number indicates the ASCII value of a character in decimal. I would like to read this file and produce three strings containing the characters from each line, in the example:


AAAB
BBC
CDE

How can I do this using Qt classes?

ttimt
28th November 2012, 06:40
ya,
string1 = AAAB;
string2 = bbc
string3 = cde

ChrisW67
28th November 2012, 06:49
QStringList result;
QFile input("input.txt");
if (input.open(QIODevice::ReadOnly)) {
while (!input.atEnd()) {
QByteArray line = input.readLine().trimmed();
QString s;
foreach(QByteArray num, line.split(' ')) {
s.append( QChar(num.toShort()) );
}
result << s;
}
}
qDebug() << result;

ttimt
28th November 2012, 07:05
i want it to appear on a lineedit,
ui->lineEdit->setText(s);
ui->lineEdit->setText(result);
neither works
error: no matching function for call to 'QLineEdit::setText(QStringList&)'

and, i'm newbie. do i need to use this codes 3 times? the QStringList is aaab, bbc or cde or all?

(im using gui, so qDebug() wont work here right?)

amleto
28th November 2012, 07:08
lineedit is for showing one string.

if you want to show string on multiple lines then use a textedit

ttimt
28th November 2012, 07:15
i want the first string (AAA) to show on aLine;
2nd string (BBC) to show on bLine;
3rd string (CDE) to show on cLine;

amleto
28th November 2012, 07:23
so look at QStringList documents and figure out how you can get first string out from it. Then think about the rest

ChrisW67
28th November 2012, 08:06
It is an example, not a cut and paste solution.

QStringList (http://qt-project.org/doc/qt-4.8/qstringlist.html), as the name implies, is a QList (http://qt-project.org/doc/qt-4.8/qlist.html) of QString (http://qt-project.org/doc/qt-4.8/qstring.html)s. Like any QList you can access one of the elements using QList::at() (http://qt-project.org/doc/qt-4.8/qlist.html#at). Your three strings are the first three elements in the list, i.e. 0, 1, and 2.

As a newbie you should learn where to find Qt Assistant, or the Help in Qt Creator, and read it.

ttimt
28th November 2012, 08:19
well, sometimes the help does not provide example its hard to understand.



QStringList result;
QFile input("configp.dll");
if (input.open(QIODevice::ReadOnly)) {
while (!input.atEnd()) {
QByteArray line = input.readLine().trimmed();
QString s;

foreach(QByteArray num, line.split(' ')) {
s.append( QChar(num.toShort()) );

}
result << s;
ui->aLine->setText(result::at(1));
}
}else

runtime error

if changed to
ui->aLine->setText(result.QList::at(1));
ERROR : template<class T> class QList' used without template parameters

wysota
28th November 2012, 09:15
well, sometimes the help does not provide example its hard to understand.

The help is not an almanach of examples, it's a means to learn how things work by reading it and applying the knowledge gained in practise in your own code.

The error you got is a C++ error, Qt help has nothing to do with this. When you get that fixed, analyze your code and think what will be the ultimate result of your code. I'll simplify it for you so it's easier to read:


QStringList result;
result << "something" << "somethingelse";
while(!input.atEnd()) {
ui->aLine->setText(result.at(1));
}

Please focus on subsequent iterations of the while loop.

ttimt
28th November 2012, 15:45
hmmm.... it seems the problem is QStringList results have nothing in it? Hopefully im right
And when i use canReadLine(), the program actually outputted cant read line



QStringList result;
result << "Something" << "abc";
QFile input("configp.dll");

if (input.open(QIODevice::ReadOnly )) {

if(input.canReadLine()){

ui->hLine->setText("can read");

}else{

//EDITED : removed this line QApplication::processEvents();
ui->hLine->setText("cant read line"); //outputted this " can't read line "
}

while (!input.atEnd()){

QByteArray line = input.readLine().simplified(); //trimmed to simplified
QString foo; //actually which line of code chris provided converted ascii numbers to char?

foreach(QByteArray num, line.split(' ')) {
foo.append( QChar(num.toShort()) ); // is it this .toShort?
}
result << foo;// if use line->setText() here also output same..
}
ui->aLine->setText(result.at(0)); // this output Something

ui->fLine->setText(result.at(1)); //this output abc not anything from the file



Edited : if i add these code below, it actually shows "can read"



if (input.open(QIODevice::ReadOnly )) {
char c;
input.getChar(&c);
if(input.canReadLine()){
ui->hLine->setText("can read");
}else{
ui->hLine->setText("cant read line");
}


EDITED: AND HEY, AFTER HOURS OF CODING, I MANGED TO LET IT shows up on aLine But (didn't know i put it inside while loop)after showing up, it still goes runtime error, so i guess i gonna figure out what wysota(hope i spell it correct) is saying

EDITED: I actually found i duplicated settext that caused error. it's working now but figuring out how to output only available line. By the way, will moderator ban for too many edits? :p I still have some problems with rename. qdir wont work too

EDITED: it now shows runtime error again.
EDITED: i actually jumped setText() from 0 to 3 where i only wanna jump 0 to 2, NOTE the rename still doesn't work :(



QFile chk("configp.dll");
chk.open(QIODevice::ReadOnly);
if(chk.exists()){

QFile exs("configp1.dll");
if(exs.open(QIODevice::ReadOnly))
exs.remove(); //this can delete, works very well


chk.rename("configp1.dll"); // it wont rename

}else{