![]() |
|
#1
|
|||
|
|||
reading a .txtI was wondering if there is a way to read information from a .txt and place it into the code just as if it had been written there to begin with, short of just using #include "new.txt". I've looked through the ifstream uses but can't seem to figure this out.
|
|
#2
|
|||
|
|||
|
What exactly will be in the .txt file? Remember that once you've your program compiled, you cannot add bits to it. You can only get the program to open files, and read their input.
GF |
|
#3
|
|||
|
|||
|
The contents of the file is basically,
Width = 800; Height = 600; It's set up right now so that when you choose the resolution, it writes what you choose into a file. I was wondering if there was a way to read from the file, even though it has changes, and without recompiling. |
|
#4
|
|||
|
|||
|
I just realized that if I knew how to just change the window resolution, it would work better than writing it to a file. Unfortuantely I'm still pretty new at all this. It's a DirectX 9.0 program if that helps you at all.
Thanks for the help |
|
#5
|
||||
|
||||
|
bengaltgrs.
It appears that you were on the wrong track. It sounds like you wanted a program to read the resolution from a text file at run time. A compiler directive, ie #include, is just that. It directs the compiler what to do and has nothing to do with run-time. |
|
#6
|
|||
|
|||
|
I've tried using #include, but every time I run the program and write something new to the .txt, it tells me the the .txt is out of date and the program needs to be re-compiled.
|
|
#7
|
||||
|
||||
|
Quote:
Exactly. When a program is compiled, the contents of the #include file is made a part of the compiled file. Therefore, whatever was in that file at compile time is what will be in your program. And only by recompiling it will the new text file be a part of your program. It really sounds like you want a configuration type file that is read in at run-time. If this is what you are after, I've got some really basic code that will read/write a configuration file. |
|
#8
|
|||
|
|||
|
That sounds exactly like what I need, if I could get that from you I think it would help a lot. Thanks for your help.
|
|
#9
|
||||
|
||||
|
Bengaltgrs:
I have hacked together some of the stuff that I have used in the past. You will need to test this yourself as I did this on the fly. It should work, but no guarantees. If you are going to use this, you should really add some error checking. It is rather rudimentary. Also, this is may seem kind of long, but most of it could be put in a library to be reused and then only the few lines at the bottom would need to be used. There are other ways of doing this, but they involve more coding, etc. Anyone that has any comments or suggestions, they are definitely welcome. Also, if anyone makes this more robust and usable, I would like it if they sent it back to me. Alrighty then. First of all, this needs a small list routine that I wrote. This is all inline, so I just put this in a .h file and there is no library to compile: list1.h CPP / C++ / C Code:
Then a structure to hold configuration items is needed Configuration Structure CPP / C++ / C Code:
Next, there a few routines that are needed to read, write & parse a configuration file. Parse a configuration list CPP / C++ / C Code:
Read a configuration file CPP / C++ / C Code:
Write a configuration file CPP / C++ / C Code:
That is the basic library, now this is the custom code that you would write to use this... Your code to read a configuration file: CPP / C++ / C Code:
To add a configuration entry: CPP / C++ / C Code:
And to write a file CPP / C++ / C Code:
Hopefully, this will give you a starting point. Adjust/modify as you see fit for your application. |
Recent GIDBlog
Developing GUIs with wxPython (Part 3) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Reading from file | jmenendezr | C++ Forum | 2 | 26-Feb-2006 16:00 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The