Compare a string with a file.
Please support our C++ advertiser: Programming Forums
Thread Solved
![]() |
•
•
Posts: 1
Reputation:
Solved Threads: 0
Hi,
I'm quite new to C++ so all help is appreciated.
I have a program that makes a string of at least three characters. I want to be able to compare the string to a list of words (i.e. dictionary) in a separate file, and then output the string if it is in the dictionary.
The string 'word' is a string object so I'm having problems with strcmp (which only takes chars?). The reason I used a string object is because I have to use substr and concatenation, and I'm not very familiar with using char arrays. (I've mostly done Java before).
What I have got so far:
Thanks in advance,
Tim
I'm quite new to C++ so all help is appreciated.
I have a program that makes a string of at least three characters. I want to be able to compare the string to a list of words (i.e. dictionary) in a separate file, and then output the string if it is in the dictionary.
The string 'word' is a string object so I'm having problems with strcmp (which only takes chars?). The reason I used a string object is because I have to use substr and concatenation, and I'm not very familiar with using char arrays. (I've mostly done Java before).
What I have got so far:
char dictword[32];
ifstream dictionary("dictionary.txt");
while (dictionary>>dictword)
{
if (strcmp(dictword,word)!=0)
cout<<word<<'\n";
}Thanks in advance,
Tim
Why use char arrays when c++ provides you with strings? Mixing up C and C++ is very confusing and will require a lot of casting. So what about something like:
cpp Syntax (Toggle Plain Text)
string word = "abc"; string dictword; ifstream dictionary("dictionary.txt"); while (getline(dictionary, dictword, ' ')) { if (word == dictword) cout << "found: " << word << "\n"; }
Last edited by niek_e : May 11th, 2008 at 6:20 am.
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
![]() |
Similar Threads
Other Threads in the C++ Forum
- search in text file (C++)
- reading a text file and searching for a sentence (C++)
- how do I have txt file be read by the compiler? (C++)
- Open Text File And Sort (C#)
- problem implementing BFS using string vector (C++)
- Replace text in a file (Shell Scripting)
- String search in a file compare? (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: need serious help!!!!!!
- Next Thread: Simple aplication typing?
•
•
•
•
Views: 576 | Replies: 1 | Currently Viewing: 1 (0 members and 1 guests)






Linear Mode