View Full Version : Custom DOS Menu Program - External Config File?
MusicallyInspired
03-19-2009, 07:36 PM
A few months back I posted some threads pertaining to a custom DOS Menu program I was making in Turbo C for my DOS games on my 486. The program is working fine now but I wanted to optimize it a step further. Right now all the game menus and launching scripts are programmed right into the menu program itself. I want to change this to have it look for an external config file instead but I'm unsure how to make the program read an external file and search for specific strings in it. Like how Duke3D reads the duke3d.cfg file. Actually, I was thinking of having something similar to how ScummVM looks through its config file for specific game configurations. Like have a game entry enclosed in "[]"'s and everything underneath it be the properties like the name, the location, the launch parameters etc.
I know there are probably a few menu programs out there that do this already and I'd probably be wasting my time trying to write my own but I want to do this myself as programming experience. Any help would be appreciated.
Dopefish7590
03-19-2009, 08:50 PM
First you need to open the file, should be easy enough... Just make a "FILE" var and "fopen" it...
FILE *filevar;
filevar = fopen(<filename goes here> , "r");
Then import the file, every time you use "getc()" The character will advance one character, so:
int *currchar[];
int i = 0;
while(!feof(filevar))
{
currchar[i] = getc(filevar);
i++;
}
This will make a searchable array called "currchar". It may be rough, and an "array" may not be the first choice for most people... But it will allow you to search the contents at any time without loading the file again. You can change the above so it detects newlines and starts some other function at each newline by adding the following to the above algorithm. You can also replace the "\n" with something else if you want to search for that instead...
if(currchar[i] == '\n') { <insert code here> }
This should help you get started...
If this does not work on your 486... It is probably because I use ncurses on my system when programming, and maybe I added an ncurses-only command. However I really think that it should work if you include "conio"
MusicallyInspired
03-20-2009, 01:56 PM
Many thanks! I'll let you know how it works.
MusicallyInspired
03-20-2009, 09:37 PM
Where do I put these lines of code? When I try to compile it keeps giving me errors which I think are related to where I'm placing them in the source. Also some nonportable pointer conversion errors.
Dopefish7590
03-20-2009, 09:38 PM
What are the errors?
The code should go in its own function whereas the "FILE *filevar;" and "int *currchar[];" should be with your declarations near the top of the source code.
MusicallyInspired
03-20-2009, 10:02 PM
Ah it wasn't a placement issue. It was a pointer issue. It wouldn't let me declare int *currchar[]; because its size was unknown and therefore wouldn't let me use it in the code later on saying it was a nonportable pointer conversion. How should I fix this? Can I just do it without using the currchar[]? Why should I use a pointer array anyway?
Dopefish7590
03-20-2009, 10:35 PM
Well you could get rid of the array entirely, but that would mean you would need to search for strings on-the-fly... But it might fix your problem.
Another solution is to define currchar with a size, the only problem that would arise is when the limit is reached.
MusicallyInspired
03-20-2009, 11:14 PM
Is there anyway to check the number of chars in the file (size of the file I guess) so I can make that number the maximum size of the array?
Also what's wrong with searching for strings on-the-fly?
Dopefish7590
03-21-2009, 10:47 AM
Is there anyway to check the number of chars in the file (size of the file I guess) so I can make that number the maximum size of the array?
Remeber "getc();"? and that whole "while" statement... The variable "i" in the code I gave you should accurately show the size needed. If you are going for filesize though. Remember there is 8 bits in one byte.
Also what's wrong with searching for strings on-the-fly?
Nothing really... It just means you have to read the file again if you want to search the file in another part of your program.
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.