File handling problems again.

Started by
9 comments, last by Rimrok 21 years, 11 months ago
This is supposed to look for an object (Object) in a text file. First of all (using geline) I look for the room. Then I take in the object name (which stands under the room name in the textfile). If Object = objecttxt, then I take in the object info and print it to the screen. I can compile it without an error, but it does''t work when I run it (or rather, it does nothing). I''ve checked all the strings and they contain the correct information when the code that I''ve included here is initiated. What am I doing wrong? (ifstream fhi) while (GetInfoCheck == true){ getline(fhi, temp, ''\n''); if (temp == currentroom){ fhi >> objecttxt; if (objecttxt == Object){ GetInfoCheck = false; getline(fin, temp, ''*''); system("cls"); cout << room.strRoomDescription; cout << "\n\n\n\n" << temp; }
The way to the right path is through pain, sweat and tears.
Advertisement
Are you using char* or std::string for your strings? If you use char arrays, you can''t compare them with ==. Use strcmp instead.
---visit #directxdev on afternet <- not just for directx, despite the name
I am using strcmp.... the compiling runs smooth without any error.. It''s kinda disturbing
The way to the right path is through pain, sweat and tears.
Is your file opening correctly? Are you error checking it?
quote:Original post by Rimrok
I am using strcmp.... the compiling runs smooth without any error.. It''s kinda disturbing

.... And you know that strcmp returns 0 (i.e. false) when the strings are identical? *just checking*
/Ksero
Is that the *actual* code that you have posted? Can you produce a small compilable example of the code that is causing a problem? If so, then post it here and we''ll have a better chance of spotting your problem. There''s too many unknowns in what you''ve posted so far.

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]


catfoodgood: Yes it is opening correctly, and how do I error check it accept checking the messages?

Ksero: Actually, I don''t know what I''m talking about, just guessing.

SabreMan: Ok, but how do I put the code in thoose code windows I''ve seen people doing here?


*Feeling like a total idiot*
But, if you don''t ask, you don''t learn!

Rimrok, I wasn''t trying to be disparaging. When helping someone on a board like this, it''s difficult/impossible to know how much the other person knows. I was just trying to be helpful, though in hindsight, maybe it was a bit too simple...
And the source boxes... check out the forum faq... you write
]source[
source goes here!
]/source[
just flip the brackets around, like []
/Ksero
Ksero: Don''t missunderstand me! I''m really greatful for all help I can get! I made fun of myself, because I really didn''t know what I was talking about when I said that I''m using strcmp, I''m that much of a newbie


Anyway, here''s the code:

First of all you need to create the test.txt file.
It should look like this:

RoomName
ObjectName
ObjectDiscription*





  #include <iostream> #include <fstream>#include <string>#include <windows.h>using namespace std;void main(){ifstream fget;bool GetInfoCheck = true;string temp, currentroom, objecttxt, object, objectdiscription;fget.open("test.txt");if(fget.fail()){  cout << "There is something wrong with the .txt file!"; return;}cout << "Which room do you whant to search in? \n\n: ";cin >> currentroom;//Now Iv''e got the room name that I whant to search for. cout << "\n\n What do you whant to look for? \n\n: ";cin >> object;					//Gets the object to search for.//Doh					while (GetInfoCheck == true){getline(fget, temp, ''\n'');	if (temp == currentroom){	fget >> objecttxt;		if (objecttxt == object){		GetInfoCheck = false;		getline(fget, objectdiscription, ''*'');		system("cls");		cout << "\n\n\n\n" << objectdiscription;		}		else if(objecttxt == "<objend>"){ cout << "\n\nCouldn''t find that object in " << currentroom <<  "."; Sleep(1500); GetInfoCheck = false;}	}	else if(temp == "<end>"){ cout << "\n\nCouldn''t find " << currentroom <<  "."; Sleep(1500); GetInfoCheck = false;}}fget.close();}  



The way to the right path is through pain, sweat and tears.





Recently:
Ok, now this is disturbing... The code that I've posted here runs flawless. But it won't work when I use it as a function in a larger project. I give the function the name of the room and the name of the object, but it can't find it... The wierd thing is, when i print the name of the room/object inside the function, they look like they are supposed to... hmm...


I've solved it now... Havn't got the time to explain...

[edited by - Rimrok on May 2, 2002 12:51:48 PM]
The way to the right path is through pain, sweat and tears.

This topic is closed to new replies.

Advertisement