Sign in to follow this  

Character Array parsing tutorials?

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

Anyone know any good character array parsing tutorials? I'm trying to load files into char buffers so I can parse them, but I have no idea how to go about it. I'm not looking for a listing of string functions, but some examples of putting them all together. (I'm using C++/MinGW)

Share this post


Link to post
Share on other sites
In C++ you can load a file into an std::string (one of C++'s native string types) in a few lines of code.


#include<sstream>
#include<fstream>
#include<string>

int main()
{
std::stringstream ss;
std::ifstream file("myfile.txt");
ss << file.rdbuf();
std::string filecontents = ss.str();
}




The contents of the file are then in the filecontents string.

You could then use something like boost::tokenizer to parse the string.

Share this post


Link to post
Share on other sites
Okay, I know the difference now. :) I was writing a parser using just fstream, but everything starts to go to hell at some point, and I can never find out why. I will do an fstream.get and it will return 0 when it should be returning say '3' and I know exactly where in the file I am. :| I ave rewritten my file parser 3 times because of this...


[Edited by - Vampyre_Dark on March 9, 2005 12:56:16 AM]

Share this post


Link to post
Share on other sites
Okay, I'm using a stringstream of the file, and getting one line at a time out of it into a char array, and it's working great so far. strtok is taking me a long way. :) I'm just stumped on one part... I need to grab 2 chars at a time out of a array into another... how can I do this?

Share this post


Link to post
Share on other sites

This topic is 4667 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this