Jump to content
  • Advertisement
Sign in to follow this  
penetrator

C++ : parsing file and retrieving data

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

I have a text file structured as follows: alpha=12 beta=none gamma=77 I know how to retrieve every line with fscanf (so i get the whole line content). But what if i'd like to retrieve only what is at the right of the = sign ? How would i do that ?

Share this post


Link to post
Share on other sites
Advertisement
You could do the following:

fgets (buffer, buffer_size, file);

char *equal = strchr (buffer, '=');

the 'equal' variable will either point to the location of the first '=' character in 'buffer', i.e. *equal == '='; or it will be NULL if there is no '=' in the string.

Skizz

Share this post


Link to post
Share on other sites
fscanf is an evil backwards compatibility feature from C (unless you're writing for an embedded system, which I'm guessing you're not doing if you have to ask this).

Here's an example using the ::std namespace:

#include <iostream>
#include <fstream>

int main ( int argc , char ** argv )
{
std::string filename = "C:\\eclipse\\workspace\\test\\test.cfg";
if (argc >= 2) filename = argv[1];
std::ifstream file( filename.c_str() , std::ios::in );
std::cout << "Reading from: " << filename << std::endl;
std::string line;
while (getline( file , line ))
{
if (line.empty() || (line == "\r")) continue; //blank lines for both windows and linux ignored. This will otherwise generate an "Invalid line" bellow.
int equal_pos = line.find('=');
if (equal_pos == std::string::npos) { std::cerr << "Invalid line" << std::endl; continue; }
std::string key = line.substr(0,equal_pos);
std::string value = line.substr(equal_pos+1,line.size()-equal_pos-1);

std::cout << "Key: " << key << " " << "Value: " << value << std::endl;
}
std::cin.get();
}




Results using this config file:

pie=good


money=rootofallevil
rootbeer=godlike


(note those redundant whitespaces)

The result:

Reading from: C:\eclipse\workspace\test\test.cfg
Key: pie Value: good
Key: money Value: rootofallevil
Key: rootbeer Value: godlike

Share this post


Link to post
Share on other sites
Thx for your support ! I get this compile errors:

02102004_dogfight_ok.CPP(27265) : error C3861: 'getline': identifier not found, even with argument-dependent lookup
02102004_dogfight_ok.CPP(27267) : error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)

in the following lines:

(27265) while (getline( file , line ))
{
(27267) if (line.empty() || (line == "\r")) continue;

i don't understand why getline is not found, since i included the correct headers.

Share this post


Link to post
Share on other sites
The global std::getline is declared in <string>, so #include that at the top.

By the way, if you're not just doing this for learning, you might want to take a look at the Win32 functions: {Get,Write}PrivateProfile{Int,Section,String,...etc}. They provide parsing capabilities for INI files (the format you're parsing)

Share this post


Link to post
Share on other sites
You can use the [] scanf() converters. For example:


char line[256];
char var[256];
char value[256];
while( true ) {
line[0] = 0;
fgets( line, sizeof(line), infile );
if( !line[0] ) break;
if( 2 != sscanf( line, "%[a-zA-Z_0-9]=%[^\n]", var, value ) ) {
fprintf( stderr, "syntax error: %s", line );
}
else {
got_var_value( var, value );
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by penetrator
Thx for your support ! I get this compile errors:

02102004_dogfight_ok.CPP(27265) : error C3861: 'getline': identifier not found, even with argument-dependent lookup
02102004_dogfight_ok.CPP(27267) : error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)

in the following lines:

(27265) while (getline( file , line ))
{
(27267) if (line.empty() || (line == "\r")) continue;

i don't understand why getline is not found, since i included the correct headers.


including #include <string> as mentioned by Coder? My system includes <string> in one of those other files, and I frogot to include it for internet consumption.

Also, getline seems to be inside the std:: namespace - try std::getline( file , line ).

If including <string> dosn't fix the == problem, I think your header files are most likely messed up - but a workaround would be:

if (line.empty() || (!strcmp(line.c_str(),"\r")) continue;

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!