This topic is now archived and is closed to further replies.

File handling problems again.

This topic is 5709 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; }

Share this post

Link to post
Share on other sites
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*

Share this post

Link to post
Share on other sites
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! ]

Share this post

Link to post
Share on other sites
Guest Anonymous Poster

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!

Share this post

Link to post
Share on other sites
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 goes here!
just flip the brackets around, like []

Share this post

Link to post
Share on other sites
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:



#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;"test.txt");
if({ 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.


while (GetInfoCheck == true){
getline(fget, temp, ''\n'');
if (temp == currentroom){
fget >> objecttxt;
if (objecttxt == object){
GetInfoCheck = false;
getline(fget, objectdiscription, ''*'');
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;}

Share this post

Link to post
Share on other sites

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]

Share this post

Link to post
Share on other sites