Jump to content
  • Advertisement
Sign in to follow this  
raptorstrike

extracting c++ synax from .txt file

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

this is kind of an odd question but can you do something like this short int Foo[10][10] = "Foo.txt" Foo.txt: { { 16, 17, 17, 17, 17, 17, 17, 17, 17, 18 }, { 173, 173, 173, 173, 173, 171, 163, 163, 163, 217 }, { 173, 173, 173, 173, 216, 185, 171, 171, 163, 217 }, { 191, 231, 163, 163, 163, 216, 185, 171, 171, 217 }, { 191, 191, 231, 163, 163, 191, 216, 171, 171, 217 }, { 235, 191, 231, 231, 163, 191, 216, 171, 171, 217 }, { 235, 235, 235, 235, 235, 235, 191, 216, 171, 217 }, { 235, 235, 235, 235, 235, 191, 235, 235, 216, 217 }, { 235, 191, 191, 191, 191, 235, 235, 235, 216, 217 }, { 235, 235, 235, 235, 235, 235, 235, 235, 235, 216 } }; of course it wouldnt look like that but hopefully you get the idea thanks[smile]

Share this post


Link to post
Share on other sites
Advertisement
Quote:

short int Foo[10][10] =
#include "Foo.txt"


That couldn't possibly work because #include is a preprocessor directive and has no value. Look-up the documentation for fstream if you want to read and write to files.


oops! I'm wrong. Thank you, Fruny.

[Edited by - Kelly G on November 28, 2004 7:42:12 PM]

Share this post


Link to post
Share on other sites
i know all about fstream but it seams tedious to read in each number individualy and making sure to skip over , and {}

edit acctually come to think of it that wouldnt be that bad an idea ill just index the [][this] ever , and the [this][] every { thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Odd question indeed. Why are you doing this? Do you want to load at runtime? Compile-Time? Tell people what you are trying to do so they can help you find a better solution

Share this post


Link to post
Share on other sites
Quote:
Original post by Kelly G
That couldn't possibly work because #include is a preprocessor directive and has no value.


Wrong. The preprocessor substitutes #include directives with the actual code in the file before the compiler ever sees anything. Though it is bad practice, this will work. The preprocessor and compiler care little of whether it is a .txt file or a .h file you're #including.

Share this post


Link to post
Share on other sites
Quote:
Original post by mike25025
short int Foo[10][10] =
#include "Foo.txt"

?


That should work, but be very carefull with it. It just screams potential for untracable bugs. ie - a misplaced comma somewhere in you're txt file. I'm not sure that it would track the bug down into the txt file or not, but it may simply trace to the #include line. For you it might be obvious that that would mean an error in the txt file, but anyone else looking at your code will be scratching their heads...

Couldn't you just write up a quick little loader function instead? That way it could be re-used wherever you have to load up any array. Not only that, but making it a function means it becomes a run-time operation rather than compile time. If you need to change the data, you won't need to re-compile. My recomendation to you is to just write up a quick loader.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Though it is bad practice

Why, exactly? Having a tool output initialized C/C++ arrays in code instead of binary/textual data saves you the effort of writing a parser.

GIMP outputs C code.

Share this post


Link to post
Share on other sites
Quote:

Wrong. The preprocessor substitutes #include directives with the actual code in the file before the compiler ever sees anything. Though it is bad practice, this will work. The preprocessor and compiler care little of whether it is a .txt file or a .h file you're #including.


Ah yes. I see how it would work now. I never would have thought of that.

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!