Getting data from text in C/C++

Started by
6 comments, last by iliak 18 years, 7 months ago
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.
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.
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).
"Game Maker For Life, probably never professional thou." =)
I believe that people here are really awesome although I haven’t tried yet so far.

Thank for your helpful responses!
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

OpenGl + C++

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.
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]
Why not giving a try to xml format ?
With TinyXML it's really easy....
- Iliak -
[ ArcEngine: An open source .Net gaming framework ]
[ Dungeon Eye: An open source remake of Eye of the Beholder II ]

This topic is closed to new replies.

Advertisement