Strcmp does not recognize string1 as equal to string2

I have a problem with the format of my text file...The file is a list of words from the dictionary(27,000 words).

For whatever reason, when I search for a word with the following code, the search returns nothing unless I manually delete space("\n") around the word in the text file, and manually reinsert the space("\n"). There are 27,000 words, I'm not gonna do that for each word!

I don't know if any of that made sense but here is the code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using namespace std;
int main()
{
	string line;
	char line2[3] ;
	strcpy(line2, "is");
	char* cstr;
	
	
	ifstream myfile ("/Users/appliedstatistics/Language/dict.txt");
	if(myfile.is_open()){
			do{
			getline(myfile, line);
			cstr = new char [line.size()+1];
			strcpy (cstr, line.c_str());
			if(!strcmp(cstr, line2)){cout << cstr << endl;}
			}while(myfile.good()); 
		
		
		myfile.close();

	}
		
	else cout << "Unable to open file"; 			
	
	return 0;
}


I would like to either:

1. write this code to account for the "spaces" in the text file

or

2. write a code to read each line in the text file and copy that line into a new text file(which is formatted correctly)

or

3. re-format the entire text file

is it possible that the words in the file are not on different lines, even though they appear to be when the file is open...

any ideas would be great...
Last edited on
closed account (zb0S216C)
You cannot directly modify the contents of the file. You're gonna' have to extract each line from the file into a buffer (vector <string>). From there, you can alter the contents of each line. When your modifications are done, write all the data back into the file.

Wazzak
Last edited on
I copied each line directly into a new text file with the following code but I'm still having the same problem! Here is copy code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int main()
{
	string line, line3, line4;
	char line2[20] ;
	strcpy(line2, "greet");
	char* cstr;
	
	
	ifstream myfile ("/Users/appliedstatistics/Language/dict.txt");
	ofstream newfile ("/Users/appliedstatistics/Language/newdict.txt");
	if(myfile.is_open()){
		do{
			getline(myfile, line);
			newfile << line;
			
			//cstr = new char [line.size()+1];
			//strcpy (cstr, line.c_str());
			
			//if(!strcmp(cstr, line2)){cout << cstr << endl;}
		}while(myfile.good()); 
	}	
		myfile.close();
	
	else cout << "Unable to open file"; 			
	
	return 0;
}



This copies all lines from file "dict" to file "newdict"...

The problem is that when I search through either file strcmp doesn't recognize "line" as equal to "line2." The code I'm using to compare strings is the first code on this page...

do you think that the formatting of the txt file is the problem?
Well, you're losing your endlines when you copy....

Add an endl.

1
2
3
4
5
6
<snip>
		do{
			getline(myfile, line);
			newfile << line << endl;			
		}while(myfile.good());
<snip>

Last edited on
I figured out a solution:

Download a new wordlist...

worked like a charm! thanks!
Topic archived. No new replies allowed.