Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Reading "directly" from a file  (Read 3963 times)

0 Members and 3 Guests are viewing this topic.

ineed.help

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Reading "directly" from a file
« on: May 05, 2014, 01:12:58 pm »
Hello. I need some help with a problem I can't quite seem to solve, neither in pure C++ nor with the help of SFML.
Basically, say I have the following code.

#include <iostream>

int main()
{
}
 

And then, in a separate document called "Something.txt":

Code: [Select]
std::cout << "Hello!";

Now I want to do something like this:

int main()
{

        // somehow read from "Something.txt", so that the code written in the .txt file is entered here
        // therefore, it would write:
        // std::cout << "Hello!";
}
 

Is there any way to do this in C++ / SFML?

I'd appreciate any help I can get.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Reading "directly" from a file
« Reply #1 on: May 05, 2014, 01:15:44 pm »
I can't tell if you're asking how to simply read text from a file or not.  If so, use standard C++ filestreams.

Tutorial: http://www.cplusplus.com/doc/tutorial/files/
Reference: http://www.cplusplus.com/reference/fstream/fstream/


If you're asking how to read C++ code from a file and interpret it at runtime, you'll need a C++ interpreter.  I don't know if such a thing exists, because it doesn't make any sense to interpret C++ (most of its big advantages come from being a compiled language).  And I don't even know if it's technically possible.

Why exactly do you want to do this?  Even in dynamic languages with an eval() function, it's generally considered bad coding practice to interpret arbitrary code, much less in C++.

I guess you could achieve this by concatenating "#include <iostream>\nint main() {\n" with your text file and then "\n}\n", writing the new string to a file and then running a compiler on it and executing the resulting executable...but still, why?  You might as well just complete the code in Something.txt and compile that instead.
« Last Edit: May 05, 2014, 01:21:48 pm by Ixrec »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Reading "directly" from a file
« Reply #2 on: May 05, 2014, 01:27:31 pm »
First, please use a different forum/community (e.g. StackOverflow) for general C++ questions. There are way more experienced people in other communities, thus you might get an answer with much better content. ;)

With clang/LLVM you can do this in a way that it might get useable, but as Ixrec pointed out, it's really not the best thing to do.
If you want dynamically change the logic of you application or rather parts of your application, then you should more look into scripting languages and how to connect them with your C++ code. As such there are for example many C/C++ libraries for Lua.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ineed.help

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: Reading "directly" from a file
« Reply #3 on: May 05, 2014, 01:50:43 pm »
I can't tell if you're asking how to simply read text from a file or not.  If so, use standard C++ filestreams.

Tutorial: http://www.cplusplus.com/doc/tutorial/files/
Reference: http://www.cplusplus.com/reference/fstream/fstream/


If you're asking how to read C++ code from a file and interpret it at runtime, you'll need a C++ interpreter.  I don't know if such a thing exists, because it doesn't make any sense to interpret C++ (most of its big advantages come from being a compiled language).  And I don't even know if it's technically possible.

Why exactly do you want to do this?  Even in dynamic languages with an eval() function, it's generally considered bad coding practice to interpret arbitrary code, much less in C++.

I guess you could achieve this by concatenating "#include <iostream>\nint main() {\n" with your text file and then "\n}\n", writing the new string to a file and then running a compiler on it and executing the resulting executable...but still, why?  You might as well just complete the code in Something.txt and compile that instead.

What I want to do is read a piece of code directly from a .txt file, rather than the value of a variable - I know how to do that.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Reading "directly" from a file
« Reply #4 on: May 05, 2014, 02:07:38 pm »
Full quotes aren't needed, thus shouldn't be used.

Did you even fully read his or my answer? Because we both gave pointers into the direction of "piece of code" as well...
« Last Edit: May 05, 2014, 07:47:57 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Deathbeam

  • Jr. Member
  • **
  • Posts: 82
  • VB6, VB.NET, C#, HTML, PHP, CSS, JavaScript nerd.
    • View Profile
    • My portfolio
    • Email
Re: Reading "directly" from a file
« Reply #5 on: May 05, 2014, 02:10:54 pm »
What I want to do is read a piece of code directly from a .txt file, rather than the value of a variable - I know how to do that.
This is called scripting bro.
Spooker Framework - Open source gaming library
My portfolio
Indie Armory - Small community of a game developers. Everyone is welcome. Bring your friends, family, pets...

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Reading "directly" from a file
« Reply #6 on: May 05, 2014, 04:16:43 pm »
Basically, say I have the following code.

#include <iostream>

int main()
{
}
 

And then, in a separate document called "Something.txt":

Code: [Select]
std::cout << "Hello!";

Now I want to do something like this:

int main()
{

        // somehow read from "Something.txt", so that the code written in the .txt file is entered here
        // therefore, it would write:
        // std::cout << "Hello!";
}
 

Is there any way to do this in C++ / SFML?

If doing it at compile time is OK, then you can simply do this:

#include <iostream>

int main()
{
#include "Something.txt"
}
 

If you want runtime evaluation then perhaps look into something like lua (http://www.lua.org/) which is designed to be easy to embed into C and C++ applications and which is (IMHO) a very nice and fast language for implementing scripting in your programme.

ineed.help

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: Reading "directly" from a file
« Reply #7 on: May 05, 2014, 05:18:47 pm »
Quote
If doing it at compile time is OK, then you can simply do this:
...
This is exactly what I was looking for, thank you very much!

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Reading "directly" from a file
« Reply #8 on: May 05, 2014, 05:27:07 pm »
Quote
If doing it at compile time is OK, then you can simply do this:
...
This is exactly what I was looking for, thank you very much!
I have to ask: Why?
I'm curious as to what problem you are trying to solve with this.

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: Reading "directly" from a file
« Reply #9 on: May 05, 2014, 06:13:30 pm »
Quote
If doing it at compile time is OK, then you can simply do this:
...
This is exactly what I was looking for, thank you very much!
I have to ask: Why?
I'm curious as to what problem you are trying to solve with this.

Same here.  The only reason I'd see to do this is dynamic classes that are built at runtime. ???
I have many ideas but need the help of others to find way to make use of them.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Reading "directly" from a file
« Reply #10 on: May 05, 2014, 06:30:50 pm »
Same here.  The only reason I'd see to do this is dynamic classes that are built at runtime. ???
Everything happens at compile time, thus definitely no.

And please stop this full quoting all the time, in particular for posts that are directly above. Full quotes have become a real plague recently. Edit: I noticed I'm already the second person to say that in this thread, even worse.
« Last Edit: May 05, 2014, 06:32:44 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: