Jump to content
  • Advertisement

Archived

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

Gollum

How to read a specific line from a text file?

This topic is 6508 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

Yet another newbie question brought to you by Gollum. So, I have read the free Sams book on Informit.com. At least the chapter on streams, anyway. It seems pretty clear to me how to write to/read from a file, using the ofstream and ifstream objects. But what if you want to get a certain line from a file? Let''s say that I have a text file filled with the parameters for my Room class. It might have a room ID, followed by a description: 1"Room 1" 2"Room 2" 3"Room 3" So, if the file looks like above, and I want to go get the description string for the room with an ID of 2, how would I do it? Is there any kind of pattern matching that I can do prior to the read? Would I read in the whole file and then do some sort of string manipulation? Or would I forget about text files and start thinking about using a database? This has got to be a pretty basic concept, I imagine. Of course I''d be happy to read any relevant web tutorials; I just haven''t had any luck finding ''em. Thanks in advance for any help, - gollumgollum

Share this post


Link to post
Share on other sites
Advertisement
you can do it like this:


FILE *file = fopen( filename, "rt" );
char line[256];

while ( fgets( line, 256, file ) != NULL )
{
// skip if comment line
if ( !strncmp( line, "//", 2 ) ) continue;

if ( sscanf( line, "id: %i desc: %s", &id, &desc[20] ) == 2 )
{
roomid = id;
description = desc;
}

}


btw, i am not sure does reading a string work like that... i haven't done it anytime, but reading anykind of numbers works that way.

Edited by - 303 on November 24, 2000 3:09:52 PM

Share this post


Link to post
Share on other sites
As a choice ,you can do it as the following:

#define DESC_LENG 300
typedef struct _ROOM_INFO
{
DWORD dwID;
LPTSTR lpszDescription[DESC_LENG+1];
//the other members
}ROOM_INFO,*PROOM_INFO;
BOOL READWriteRoomInfo(DWORD dwID,PROOM_INFO pRoomInfo,BOOL bWrite)
{
FILE *fp;
fp=fopen("room.dat","rw");
if (!fp) return FALSE;
fseek(fp,dwID*sizeof(ROOM_INFO),SEEK_SET);
if (bWrite)
{ if ( fwrite(pRoomInfo,sizeof(ROOM_INFO),1,fp)!=sizeof(ROOM_INFO) )
return FALSE;
}
else
{
if ( fread(pRoomInfo,sizeof(ROOM_INFO),1,fp)!=sizeof(ROOM_INFO) )
return FALSE;

}
fclose(fp);
return TRUE;
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!