user3313840
user3313840

Reputation: 27

storing data after reading a file in a Qvector

I have a weird problem not sure why:

I made a program that checks if the user entered a valid name " which checks qvector first " if it find it then it says that it was found, the problem is since the qvector was a little big i made a txt file and put the names in there line by line like this:

name1 name2 name3 . . . .

I used this function to read it line by line using qfile

for (int i=0; !names->atEnd(); i++){
    q_names[i]=names->readLine();
    }

q_names is a qvector of QString

I checked using qline edit to display if it was copied or not and it was!! " I showed it using settext(q_names[3]) for example and it works like a charm.

now when I tried to let the user enter a string and checks if it was in the vector or not , I used this:

for(int i=0; i<50; i++){
    if(lineedit_names->text()==S_names[i]){
..}

but it doesn't work

the above is just an example hopefully it'll clear the problem, and here's some pics to know what I was talking about:

http://tinypic.com/view.php?pic=2wd7iph&s=8

http://oi61.tinypic.com/2wd7iph.jpg

when it finds it " matches " it sets the line edits down there to true and enable it

what is strange is when I enter the last value that was in the text file it enables it like there's no problem " which mean it found that value "

I'm guessing it's because of the new line ?

Upvotes: 0

Views: 408

Answers (1)

PeterSW
PeterSW

Reputation: 5261

If you're reading directly through the QFile then you are using QIODevice::readLine. The documentation for that states that it leaves the newline character in place. In that case they will be included in the comparison and you will need to remove them for the comparison you want.

If instead you use a QTextStream to read the file as shown in the QFile documentation then any newline (and carriage return if on windows) characters will be automatically trimmed.

Upvotes: 1

Related Questions