0

My text file contain

Wew213
Wew214
Wew215

and my input in program is

Wew213

but it show me output

"Not Matched"

Actually what i am doing is i want to enter the input and if input match the number in a text file it should run the output by if statement otherwise else statement

here is my program

char file_data[10];
std::ifstream file_read ("D:\\myfile.txt");
cout<<"Enter the number to search"<<endl;
char val[10];
cin>>val;
while(!file_read.eof())
{
    file_read>>file_data;
    cout<<file_data<<endl;
    }
    if (val == file_data)
    {
        cout<<"Matched"<<endl;
    }
    else
    {
           cout<<"Not Matched"<<endl;
    }
}
AHF
  • 1,070
  • 2
  • 15
  • 47
  • 4
    `while(!eof(file))` is [always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). –  Dec 08 '13 at 21:12
  • @H2CO3 I read it twice but didn't got his point , is this `while(!eof(file))` read the file one more time than it need to – AHF Dec 08 '13 at 21:37

5 Answers5

8

you are comparing the pointer value, which is different

you need to use strcmp to compare c string. or use std::string

if (strcmp(val, file_data) == 0)
{
    cout<<"Matched"<<endl;
}
else
{
       cout<<"Not Matched"<<endl;
}

or

if (std::string(val) == std::string(file_data))
{
    cout<<"Matched"<<endl;
}
else
{
       cout<<"Not Matched"<<endl;
}
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
4

The == test compares the addresses val and file_data. Instead of ==, to compare the contents of the character arrays use the function strcmp().

Simon
  • 10,679
  • 1
  • 30
  • 44
4

The given code,

char file_data[10];
std::ifstream file_read ("D:\\myfile.txt");
cout<<"Enter the number to search"<<endl;
char val[10];
cin>>val;
while(!file_read.eof())
{
    file_read>>file_data;
    cout<<file_data<<endl;
    }
    if (val == file_data)
    {
        cout<<"Matched"<<endl;
    }
    else
    {
           cout<<"Not Matched"<<endl;
    }
}

looks like this after running it through AStyle:

    char file_data[10];
    std::ifstream file_read ("D:\\myfile.txt");
    cout<<"Enter the number to search"<<endl;
    char val[10];
    cin>>val;
    while(!file_read.eof())
    {
        file_read>>file_data;
        cout<<file_data<<endl;
    }
    if (val == file_data)
    {
        cout<<"Matched"<<endl;
    }
    else
    {
        cout<<"Not Matched"<<endl;
    }
}

So, since the checking is done after the loop, checking only the last item read by the loop, even if you got the string comparison itself correct your program would not work.

The comparison doesn't work because, as others (rushing in) have already noted, you're comparing pointers, not strings.

To compare strings, use std::string instead of character arrays.


Minor correction: instead of

while(!file_read.eof())

write

while(!file_read.fail())

or just

while(file_read)

which calls fail for you (negating the result).

But doing this you would also have to check for success/failure of the input operation.

And the common idiom is to do that directly:

while( file_read>>file_data )
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
2

The == operator will simply compare the address. You will need to use strcmp function.

rami1988
  • 156
  • 4
1

character arrays have no the comparision operator. So instead of comparing arrays theirself you are comparing addresses of first elements of the arrays.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335