Visual Studio C++ question / ranting

Today i had a really simple test at school. Read from a file, one line at a time and then search for a particular string in the line. if the string is found output it to a particular file if it is not found to another file. Basically reading from one file and writing to two other files, nothing hard. Now i am used to using linux, nano, terminal and gcc/g++ and kinda that's it. The code is as follows:

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
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {

	string input_data;
	string keyword;

	ifstream InputFile;
	ofstream OutputFile;

	cout << "input the key you need: " << endl;
	cin >> keyword;

	InputFile.open("/home/blackmail/Desktop/student");

	while(!InputFile.eof()){
		getline(InputFile,input_data);
		unsigned int a = input_data.find(keyword);

		if(string::npos != a){
			OutputFile.open("/home/blackmail/Desktop/info",ios::app);
			OutputFile << input_data << "\n" ;
			OutputFile.close();
		}else{
			OutputFile.open("/home/blackmail/Desktop/altele",ios::app);
			OutputFile << input_data << "\n";
			OutputFile.close();
		}

		cout << input_data << endl;
	}

	return 0;
}


It works in linux seamlessly and in MV C++ it fails miserably, mainly because the find function from the string object is not working as the standard description. I will test this code in windows using dev c++, but in linux it works well under QT, eclipse, and terminal. If i know well anyway eclipse uses g++ so only QT has a different compiler if i know well, anyway the thing is it works. Why?

Thanks in advance for any further replies.
It works in linux seamlessly and in MV C++ it fails miserably, mainly because the find function from the string object is not working as the standard description.


Seems more likely you didn't change the path and the file isn't opening so it loops endlessly on the eof condition.
Seems more likely you didn't change the path and the file isn't opening so it loops endlessly on the eof condition.


oh but no, the path was changed to the appropriate location for the desktop, and also i included an escape sequence for every "\" in the windows style path for it to work so the path looks like this:
1
2
3
C:\\Documents and Settings\\student\\Desktop\\student.txt
C:\\Documents and Settings\\student\\Desktop\\info.txt
C:\\Documents and Settings\\student\\Desktop\\altele.txt


So the idea is that i have a problem with the find function, the file opening part works fine. I even cout the string before passing it to the if function in the windows version, just to be sure that they get read in well from the file.
No, the find() function works fine even with VS C++ (I used it alot)

I'd say it is what cire said.

Better use the following:
1
2
3
	while(!InputFile.eof()){
		getline(InputFile,input_data);
	while(getline(InputFile,input_data))


this way you don't read more lines than existing
Anyway, you are right, indeed the function is evaluated once more in the loop, and also i have made the update as you suggested, but nonetheless it still doesn't want to work. is there an implementation of find different than the standards? I searched for it but i did not find it. Now i must say i am not an expert in MS C++ so it may be that the ide needs some specific configuration but under the other platform i just simply compile it. Paths are changed and everything... i think i will just need to tokenize the string under MS, that would also be a way but for such a simple program it is like going after a fly with a bazooka...
find works just fine. So do forward slashes in path names.

Have you verified that the file is actually getting opened?

Topic archived. No new replies allowed.