Jump to content
  • Advertisement
Sign in to follow this  
GSide

Getting data from text in C/C++

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

How can I read and get the data that I want from a text file in C/C++. For example I have a text file contains the following text. OBJECT_NAME: “F-16”; VERTEX_COUNT: 9; FACE_COUNT: 3; DAMAGE: 0.0; COLLIDE: FALSE; How can I get the “F-16” and put it in: char *Object_Name; The VERTEX_COUNT: 9 in: int Vertex_Count; the DAMAGE: 0.0 in: float Damage; and the COLLIDE: FALSE in: BOOL Collide; Help me please!!! I’ll be grateful for your help.

Share this post


Link to post
Share on other sites
Advertisement
Here's the basic procedure using the Standard C++ Library.

1 - Read in a line
ifstream IF("file.txt");
std::string tempStr;
std::getline( IF, tempStr );
// tempStr == OBJECT_NAME: “F-16”;

2 - Parse the string using a simple string tokenizer for example.
std::vector<std::string> tokens = tokenize(tempStr, ": ;\"");
// tokens[0] == OBJECT_NAME
// tokens[1] == F-16

3 - Now use the data as you wish
if( tokens[0] == "OBJECT_NAME" )
objects[current]->name = tokens[1];
else if( tokens[0] == "COLLIDE" ) )
{
if( tokens[1] == "FALSE")
objects[current]->collide = 0;
}
...


Now just loop though the entire file and add in the code to handle all the cases. This is just one way of doing this.

Share this post


Link to post
Share on other sites
I used a similar approach, except that I ignore lines that start with "/"...

so...

//Speed
125
//Size
10


and so on...

then I read the stuff using what the above poster said (kind of).

Share this post


Link to post
Share on other sites
I believe that people here are really awesome although I haven’t tried yet so far.

Thank for your helpful responses!

Share this post


Link to post
Share on other sites
I tried to do the same, but my compiler doesnt recognise :resToken = tokenize(tempStr, "\t");

I inculed these lines at the top:


#include "stdafx.h"
#include <vector>
using namespace std;
#include <string>


My code is like this:

ifstream IF("file.txt");
std::string tempStr;
std::getline( IF, tempStr );

std::vector<std::string>
resToken = tokenize(tempStr, "\t");

I get this error:
test.cpp(111) : error C2065: 'tokenize' : undeclared identifier

Share this post


Link to post
Share on other sites

A really simple way, if all your data is in that format, is GetPrivateProfileString(), for reading ini files.

MUST BE:

object_name = f-16


One function call.

Share this post


Link to post
Share on other sites
I think you can try standard_io functions.
My codes is :

//
// I assume that you have a txt file in c:/test.txt ;
// Also I assume in that "test.txt" you type "name:f-16;" .
//
#include "stdio.h"
main()
{
// open the file
FILE* pf=fopen( "c:/test.txt","r" ) ;
char result[20] ;
fscanf( pf,"name:%s;",result) ;
fclose( pf ) ;
printf("the name is:%s \n",result) ;
return 0;
}

I hope you would try this codes.And I also hope the codes have some suggestion.
If the codes do not work ,please tell me.(Email:jfwf@yeah.net).
Good Luck!

[Edited by - jf_wangfeng on August 26, 2005 10:56:54 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!