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


File handling

Recommended Posts

Rimrok    122
This is supposed to look for an object(string of text) (the string is called 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
GlaZZ    122
You cannot compare two strings by using == operator, it only compares the addresses of those strings, which aren''t the same even if the string are identical.

Use strcmp(string1, string2) function, which compares the strings. If it returns zero, string1 and string2 are identical.

Share this post

Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
 #include <string>  

STL rocks!
Beside that, I''ve also had the same problem and I think I solved it this way:

Store the "initial data" of each room before the actual room-description or whatever.

01 03 06 01 01 00 00 00
| | | | |
| | | |__|_ Offset in bytes to next room
| | |
| | |_ Number of objects in this room
| |
|__|_ Room id flag

Then you don''t need to compare strings, which takes more CPU time than comparing a unsigned short. Ofcourse you''ll have to write your own converter that will remake your "source" textfiles and store them in a binary "world" file. Use fixed dimension arrays to store object names and description, then you can fit them into a struct or something similar and just read in the whole chunk of data. Saves a lot of time. You could also use a slightly more dynamic approach, when you store the lenght of each string/name before the string appears in the file and then allocate memory before reading it.

The second way (the dynamic one) is a bit harder to code, but will produce a slighly smaller world-file.

#define MAX_OBJECTS 10

typedef struct OBJECT_DATA{
char name_of_object[16];
unsigned char object_id;
char object_description[128];

typedef struct ROOM_DATA{
char name_of_room[16];
char room_description[255];
object objects[MAX_OBJECTS];

This way is the easiest, but the world-file will eventually become very large, since you write about 1700 bytes for every room (if you use byte aligned structs ) Some basic math tells you that 50 room will yield a 83 kb roomfile, which then will take some time to search through.

Share this post

Link to post
Share on other sites