Forum Archive

Go Back   3D Realms Forums > General Topics > Programming Forum
Blogs FAQ Members List Social Groups Calendar Mark Forums Read

Notices

 
 
Thread Tools
Old 05-14-2008, 07:30 AM   #1
MusicallyInspired

MusicallyInspired's Avatar
String Problems in C
I'm writing a menu program with Turbo C for my DOS computer for all my old games. It'll be a select-with-arrows-and-press-enter style button menu. I was going to go with just inputing the item numbers but that's too boring. Anyway, I haven't implemented the menu selector yet. Right now I just have it coded so that it displays the first menu and then pauses until you press a key which quits the game. I did that because I want to make sure the menu displays properly, which it doesn't.

There are 2 "buttons" that are displayed on the screen: "+)Adventure Games" and "+)Arcade Games". But when the program displays them on screen they come out like this:

+)Adventu+)Arcade Games
+)Arcade Games

and then it prints a 3rd previously undefined button underneath those named:

Games

It's somehow copying parts of each button's string onto the first button and then on the second button and third and so on as well. Am I using a string function wrong or something? I use both strcmp and strcpy.

Here's the program to see how it works for yourself if you like. And here's the source code (it might be pretty confusing, just ask me about something if you're confused as to its purpose):

I'd appreciate the help. This has been plaguing me for a couple days now.

Code:
/***** Includes ***********************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

/***** Constants **********************************************************/
#define BLANK " -BLANK "

#define BUTTONCOL1X 4 	// Button x/y coordinates
#define BUTTON1Y 4
#define BUTTON2Y 5
#define BUTTON3Y 6
#define BUTTON4Y 7
#define BUTTON5Y 8
#define BUTTON6Y 9
#define BUTTON7Y 10
#define BUTTON8Y 11
#define BUTTON9Y 12
#define BUTTON10Y 13
#define BUTTON11Y 14

#define FOLDER 1        // Button types
#define FILE 0

#define MAINMENU 0	// Menus

#define ADVENTURE 1
#define ARCADE 2

#define SIEADV 3
#define LUCASADV 4
#define MICROADV 5
#define OTHERADV 6

#define SIEARC 7
#define LEMMINGS 8
#define APOGEE 9
#define ID 10

#define KQC 11
#define SQCOL 12
#define PQCOL 13
#define QGANTH 14
#define LSLCOL 15
#define GKCOL 16
#define CONQUEST 17
#define ECO 18
#define DRBRAIN 19
#define MANHUNTER 20
#define DYNAMIX 21
#define COKTEL 22


/***** Declared functions used in program *********************************/
void DisplayMenu(int menu);
void MainMenu(void);
void AdvMenu(void);
void ArcMenu(void);
void Selector(int butNum);
void SetupButtons(void);
void MakeButton(const char *s,int butNum);
void PrintMenuTitle(const char *);
void ClearWindow(void);
void DrawWindow(void);

/***** Global variables used in program ***********************************/
int iCurrentMenu=MAINMENU;    // Variable for which menu is being displayed
int iMenuItem;		      // Variable for which menu item is selected
int iNumOfButtons;	      // Variable for how many buttons are in the
			      // current menu
int QuitMenu=0;

char *sButton1=BLANK;
char *sButton2=BLANK;
char *sButton3=BLANK;
char *sButton4=BLANK;
char *sButton5=BLANK;
char *sButton6=BLANK;
char *sButton7=BLANK;
char *sButton8=BLANK;
char *sButton9=BLANK;
char *sButton10=BLANK;
char *sButton11=BLANK;

/**************************************************************************
 *     Program start                                                      *
 **************************************************************************/
int main()
{
//	for(;;) {

		/***** Build the main menu window *******/
		_setcursortype(_NOCURSOR);
		DrawWindow();
		DisplayMenu(iCurrentMenu);
//		Selector();
		getch();
//		if(QuitMenu==1) {
//			break;
//		}
//	}

	/***** Program quit *********************/
	fflush(stdin);
	textcolor(LIGHTGRAY);
	textbackground(BLACK);
	_setcursortype(_NORMALCURSOR);
	clrscr();
	return(0);
}
void DisplayMenu(int menu)
{
	switch(menu) {
		case MAINMENU:
			MainMenu();
			break;
		case ADVENTURE:
			AdvMenu();
			break;
		case ARCADE:
			ArcMenu();
			break;
	}
	SetupButtons();
}
void MainMenu()
{
	PrintMenuTitle("MAIN MENU");

	MakeButton("+)Adventure Games",1);
	MakeButton("+)Arcade Games",2);
//	MakeButton("Microsoft Edit",3);
}
void AdvMenu()
{
	PrintMenuTitle("ADVENTURE GAMES");
}
void ArcMenu()
{
	PrintMenuTitle("ARCADE GAMES");
}
void Selector(int butNum)
{
	textbackground(LIGHTGRAY);
	textcolor(WHITE);

	switch(butNum) {
		case 1:
			gotoxy(BUTTONCOL1X,BUTTON1Y);
			cprintf("%s",sButton1);
			break;
	}

	textbackground(BLUE);
	textcolor(YELLOW);
}
void SetupButtons(void)
{
	textcolor(YELLOW);
	if(strcmp(sButton1,BLANK)!=0) {
		gotoxy(BUTTONCOL1X,BUTTON1Y);
		cprintf("%s",sButton1);
	}
	if(strcmp(sButton2,BLANK)!=0) {
		gotoxy(BUTTONCOL1X,BUTTON2Y);
		cprintf("%s",sButton2);
	}
	if(strcmp(sButton3,BLANK)!=0) {
		gotoxy(BUTTONCOL1X,BUTTON3Y);
		cprintf("%s",sButton3);
	}
	if(strcmp(sButton4,BLANK)!=0) {
		gotoxy(BUTTONCOL1X,BUTTON4Y);
		cprintf("%s",sButton4);
	}
	if(strcmp(sButton5,BLANK)!=0) {
		gotoxy(BUTTONCOL1X,BUTTON5Y);
		cprintf("%s",sButton5);
	}
	if(strcmp(sButton6,BLANK)!=0) {
		gotoxy(BUTTONCOL1X,BUTTON6Y);
		cprintf("%s",sButton6);
	}
	if(strcmp(sButton7,BLANK)!=0) {
		gotoxy(BUTTONCOL1X,BUTTON7Y);
		cprintf("%s",sButton7);
	}
	if(strcmp(sButton8,BLANK)!=0) {
		gotoxy(BUTTONCOL1X,BUTTON8Y);
		cprintf("%s",sButton8);
	}
	if(strcmp(sButton9,BLANK)!=0) {
		gotoxy(BUTTONCOL1X,BUTTON9Y);
		cprintf("%s",sButton9);
	}
	if(strcmp(sButton10,BLANK)!=0) {
		gotoxy(BUTTONCOL1X,BUTTON10Y);
		cprintf("%s",sButton10);
	}
	if(strcmp(sButton11,BLANK)!=0) {
		gotoxy(BUTTONCOL1X,BUTTON11Y);
		cprintf("%s",sButton11);
	}
}
void MakeButton(const char *s,int butNum)
{
	switch(butNum) {
		case 1:
			strcpy(sButton1,s);
			break;
		case 2:
			strcpy(sButton2,s);
			break;
		case 3:
			strcpy(sButton3,s);
			break;
		case 4:
			strcpy(sButton4,s);
			break;
		case 5:
			strcpy(sButton5,s);
			break;
		case 6:
			strcpy(sButton6,s);
			break;
		case 7:
			strcpy(sButton7,s);
			break;
		case 8:
			strcpy(sButton8,s);
			break;
		case 9:
			strcpy(sButton9,s);
			break;
		case 10:
			strcpy(sButton10,s);
			break;
		case 11:
			strcpy(sButton11,s);
			break;
	}
}
void PrintMenuTitle(const char *s)
{
	int l;		// Character length of title.
	int xpos;	// Starting x coordinate of centered title.

	l=strlen(s);    // Assign character length of title to 'l'.
	xpos=(80-l)/2;  // Assign starting x coordinate of centered
			// title text to 'xpos'.

	/***** Prints the current menu's title in the title bar *****/
	gotoxy(xpos,2);
	textcolor(YELLOW);
	cprintf("%s",s);
}
void ClearWindow(void)
{
	int iY;
	int iX;

	gotoxy(BUTTONCOL1X,BUTTON1Y);
	for(iY=BUTTON1Y;iY<22;iY++) {
		for(iX=BUTTONCOL1X;iX<(80-BUTTONCOL1X-1);iX++) {
			cprintf(" ");
		}
	}
}
void DrawWindow()
{
	int i=0;

	textcolor(WHITE);
	textbackground(BLUE);
	clrscr();

	/***** Draw the menu title cel *******************************/
	gotoxy(0,0);
	cprintf("");
	for (i=0;i<78;i++) cprintf("");
	cprintf("");
	for (i=0;i<78;i++) cprintf(" ");
	cprintf("");
	for (i=0;i<78;i++) cprintf("");
	cprintf("");

	/***** Draw the left wall of the whole window ****************/
	for(i=wherey();i<24;i++) {
		gotoxy(1,i);
		cprintf("");
	}

	/***** Draw the right wall of the whole window ***************/
	gotoxy(80,4);
	for(i=wherey();i<24;i++) {
		gotoxy(80,i);
		cprintf("");
	}

        /***** Draw the bottom border of the window ******************/
	gotoxy(1,24);
	cprintf("");
	for(i=wherex();i<80;i++) cprintf("");
	cprintf("");
}
__________________
Roland SC-55 Music Packs Website
- MediaFire mirror for Duke3D expansions
More SC-55 music packs coming soon...
Last edited by MusicallyInspired; 05-14-2008 at 07:32 AM.
MusicallyInspired is offline  
Old 05-14-2008, 10:44 AM   #2
Qbix
DOSBox Advisor

Qbix's Avatar
Re: String Problems in C
At first glance, might be wrong:

the make_button code is indirectly to blame
the problem is that sButton are pointers to chars instead of arrays of chars.
so you are copying the string "s" into "BLANK"
The problem is that there is only one BLANK. (and you overwrite it contents on each call)
Solving it with introducing more BLANKS is not the way to go as you are then limited by the size of the initial BLANKS on what you can copy into it.
Qbix is offline  
Old 05-14-2008, 11:03 AM   #3
rg3
Re: String Problems in C
I can't compile or run the program in the machine I'm using now, but you are doing several nasty things and some other areas could be improved. First off, you are defining a macro named BLANK which is going to be replaced by the constant string " - BLANK ". You are then creating a group of char pointers, sButton[1 to 11], and you are initializing them to have the same value as the constant string " - BLANK ". This, in itself, is wrong, because " - BLANK " is a contant string, and you are creating a non-const pointer to it, making it possible to modify its contents, which is wrong. Second, you go ahead and do modify its contents when you create the menu buttons, using strcpy. That part of the program is completely broken. How much broken? That depends on the compiler. You can be overflowing the space allocated for " - BLANK " (notice that "+)Adventure Games" is much longer), you can be overwriting the values of every other string if the compiler decides to save space by using the same " - BLANK " string for every sButton variable (which is perfectly legal). In other words, a complete disaster.

The solution to that part, given that every string the program is going to use is a constant string, could be the following one:

First, don't use macros. Declare all your texts once in the form of const char arrays, like this:

Code:
const char something[] = "SOME TEXT";
Second, if you sometimes need to print that text and want to have a variable point to the right text, you can declare a const char pointer variable and make it point to the text you want, directly, without strcpy, like this:

Code:
const char foo[] = "FOO";
const char bar[] = "BAR";
const char *pointer;
/* ... */
pointer = foo;
pointer = bar;
And, to "disable" the pointer, assign it NULL, and check that before using it.

Finally, you are using 11 pointers and giving them different variable names, sButton1 through sButton11. That's why you have to write the code like in the SetupButtons() function. Do something better: use an array of pointers and iterate over it with a loop instead of duplicating so much code:

Code:
#define NUMBUTTONS 11
/* ... */
const char *sButton[NUMBUTTONS];
/* ... */
int ii;
for (ii = 0; ii < NUMBUTTONS; ++ii) {
        /* do something with sButton[ii] */
}
rg3 is offline  
Old 05-14-2008, 01:49 PM   #4
MusicallyInspired

MusicallyInspired's Avatar
Re: String Problems in C
Interesting. I understand part of everything you guys said but I'm really just a beginner and only going by what I know. I don't know a thing about pointers yet. I was trying to make string variables with those *sButtons. Turbo C didn't seem to like it when I declared them as "char[] sButton;". Each sButton is supposed to contain the string for the name of each button. What I was trying to do was make each sButton blank until a certain menu defines them otherwise (like MainMenu() or AdvMenu()). And in the SetupButtons() function I was trying to make it display only the menu items that weren't equal to BLANK (string of 8 spaces). I tried doing it in a loop but that doesn't work because for each sButton I need to do different things rather than the same thing to each sButton.

I guess I did things the wrong way. I'll try the things you've mentioned and see if I can clean it all up. Thanks for the help. I learn more everyday about programming.

Also, how exactly do you split a program up into multiple modules and compile them all at once? I think it would help organize my program a little better.
__________________
Roland SC-55 Music Packs Website
- MediaFire mirror for Duke3D expansions
More SC-55 music packs coming soon...
Last edited by MusicallyInspired; 05-14-2008 at 02:56 PM.
MusicallyInspired is offline  
Old 05-14-2008, 03:52 PM   #5
rg3
Re: String Problems in C
Quote:
Interesting. I understand part of everything you guys said but I'm really just a beginner and only going by what I know. I don't know a thing about pointers yet. I was trying to make string variables with those *sButtons. Turbo C didn't seem to like it when I declared them as "char[] sButton;".
In C, arrays, pointers and strings belong to the same family. It is important to understand their concepts before starting to use them, both the things they have in common and the things that make them slightly different. If you don't understand arrays, pointers and strings, you'll never be a decent C programmer, so take your time learning how things work.

By the way, "char[] sButton;" is not syntactically valid. Just try to do what I said. Declare all strings like const char the_string[] = "String Constant"; and make the sButtons "const char *sButton;". Then, instead of copying strings with strcpy(), use asignments as in "sButton1 = the_string;". Initialize them to NULL as in "const char *sButton1 = NULL;", and check if they don't have a value with the condition "if (sButton1 == NULL)", for example. Finally, asign them NULL if you don't want them to have a value, like in "sButton1 = NULL;". Those few replacements will get you going until you fully understand what you are doing.

Quote:
Also, how exactly do you split a program up into multiple modules and compile them all at once? I think it would help organize my program a little better.
That depends on the programming environment, but the general concept is to try to create several .c files, putting in each one of them a family of functions used for related operations, and creating .h files containing the prototypes and data type declarations. Then, include those header files when you need to use either the datatypes or functions in another .c file, and build them all together. Think of how you have a header file called string.h, specifying the prototypes of many functions related to string operations. Those functions are then implemented somewhere else and, when building your program, you include the string.h header so the compiler knows their interface. As they are part of the standard library, you don't actually get to see the implementation of those functions being compiled and linked together with the rest of your program, but the steps are there somehow.

To compile several source files together... In many programming environments, it's a matter of creating a "project" and then adding the header and source files to them, and it will take care of compiling all together.
Last edited by rg3; 05-14-2008 at 04:04 PM.
rg3 is offline  
Old 05-15-2008, 02:40 AM   #6
Qbix
DOSBox Advisor

Qbix's Avatar
Re: String Problems in C
Quote:
Originally Posted by rg3 View Post
In C, arrays, pointers and strings belong to the same family. It is important to understand their concepts before starting to use them, both the things they have in common and the things that make them slightly different. If you don't understand arrays, pointers and strings, you'll never be a decent C programmer, so take your time learning how things work.

By the way, "char[] sButton;" is not syntactically valid. Just try to do what I said. Declare all strings like const char the_string[] = "String Constant"; and make the sButtons "const char *sButton;". Then, instead of copying strings with strcpy(), use asignments as in "sButton1 = the_string;". Initialize them to NULL as in "const char *sButton1 = NULL;", and check if they don't have a value with the condition "if (sButton1 == NULL)", for example. Finally, asign them NULL if you don't want them to have a value, like in "sButton1 = NULL;". Those few replacements will get you going until you fully understand what you are doing.
The solution presented here, makes use of pointers of which the concept might be confusing for you.
If you want to use strings and copy data in them (as that is conceptionally easier, (not better, or worse)) you can do
"char[1024] sButton = { 0 };
and copy stuff into with strcpy and compare the contents with strcmp.

Please note that while pointers, arrays and strings (ASCII, zero terminitated) have familiarities, they can not be used interchangeable. So forget about the family part as else you might make mistakes like you did in your original code. (overwriting constant data (the " - BLANK" stuff), that is a serious mistake (and while it sometimes might work, it is wrong ))
The " - BLANK" is a string, the copy operation can have as destination an array, (or a pointer, but let's not make things more confusing), but not a string (which is what you did (the compiler didn't complain as you assigned that string to a pointer,(which is allowed as well (the char* SButton = " - BLANK")))
Qbix is offline  
Old 05-15-2008, 06:38 AM   #7
MusicallyInspired

MusicallyInspired's Avatar
Re: String Problems in C
I ended up going rg3's way and made a pointer array and that works. My menu displays fine now. It took a while to wrap my brain around what was happening with the pointers with that new code but I got it working. I'm just going to have it display another menu after the main one just to make sure it works when viewing multiple menus.

All I need now is to program the menu selector (which highlights the menu button it's currently on by changing the textcolor and textbackground and re-cprintf()-ing it) and what happens when you press enter on each button. Wow.....that's going to be a lot of code in that function the way I'm currently thinking of how it will work. Is there any way I can trim that down even though I've got different actions to take for each button? Right now I'm just envisioning a huge switch statement.
__________________
Roland SC-55 Music Packs Website
- MediaFire mirror for Duke3D expansions
More SC-55 music packs coming soon...
MusicallyInspired is offline  
Old 05-15-2008, 06:48 AM   #8
Qbix
DOSBox Advisor

Qbix's Avatar
Re: String Problems in C
well for the current code you can do a few things to reduce its size
1) make all those defines into an enum
2) instead of having button1.... button12 make it an array char* button[12]
then you can loop through it instead of writing them all out. (second code example in rg3 first post)

but please don't think that this is a lot of code.

for a menu with entries and such you can keep it quite small by changing it into a state machine.
Qbix is offline  
Old 05-15-2008, 07:21 AM   #9
MusicallyInspired

MusicallyInspired's Avatar
Re: String Problems in C
I did make the char *sButton[] array. I said that in the beginning of my post. It works.

The only thing I don't understand about using loops for each sButton is I have to send the cursor to different positions depending on which button is selected. How do I do that in a loop? Because I'm not just modifying sButton[i] I'm doing separate actions unique to each sButton[i]. The only way I can think of to do that is to make a switch statement. But then I'd have to make one for every button wouldn't I? Like comparing whether or not the 1st button is selected (stored in iMenuItem) and what happens when I press left, right, up, down (those four of which would send the cursor to unique positions depending on what buttons is selected), enter (also of which performs a unique action depending on which button is selected) and then the same thing for button 2 and so on. It's a lot of repeating code with just the gotoxy() values changed and the system() calls being different. I'm just wondering if there's a more efficient way of doing that.
__________________
Roland SC-55 Music Packs Website
- MediaFire mirror for Duke3D expansions
More SC-55 music packs coming soon...
Last edited by MusicallyInspired; 05-15-2008 at 07:24 AM.
MusicallyInspired is offline  
Old 05-15-2008, 08:08 AM   #10
Qbix
DOSBox Advisor

Qbix's Avatar
Re: String Problems in C
store the actions in an array ? (you are getting close to statemachine then )
first loop to find the array index (from the button[])
and the perform the action in table actions with the index from the array of button.
Qbix is offline  
Old 05-15-2008, 09:22 AM   #11
MusicallyInspired

MusicallyInspired's Avatar
Re: String Problems in C
What if I made pointer arrays for the X and Y coordinates of each button? I've already made one for the button names (strings) with const char *iButton[BUTSLOTS] (where BUTSLOTS is the total number of buttons allowed in the menu at one time which is 57). I'm currently trying to make const int *ButtonX[BUTSLOTS] and const int *iButtonY[BUTSLOTS], but the compiler won't accept these pointers as int values in the gotoxy() function. I made sure to define them before they are used. How come it works for char's but not int's?

EDIT: I see the problem is stemming from the attempts to define them themselves. iButtonY[1]=BUT1; won't work. It says "Nonportable pointer conversion." I even tried replacing BUT1 with the actual number "4" and it still wouldn't work. I guess I'm using it wrong but I'm still not grasping why.

EDIT 2: Actually I just made it a regular int variable array instead of a const int pointer array and that seems to work.
__________________
Roland SC-55 Music Packs Website
- MediaFire mirror for Duke3D expansions
More SC-55 music packs coming soon...
Last edited by MusicallyInspired; 05-15-2008 at 09:52 AM.
MusicallyInspired is offline  
Old 05-15-2008, 10:29 AM   #12
rg3
Re: String Problems in C
Quote:
store the actions in an array ? (you are getting close to statemachine then)
Warning! Warning! Function pointers ahead . (I agree that's a good idea, though).
rg3 is offline  
Old 05-15-2008, 03:25 PM   #13
MusicallyInspired

MusicallyInspired's Avatar
Re: String Problems in C
Ok, after much toil this is my current source code that works so far (here's the EXE). The only problem I've run into is that when it launches some games they won't work because the menu program is taking up too much base memory lol. Sort of defeats the purpose. Unless there's some kind of way around that. Is there anything I'm doing wrong that I can fix? Or re-code to work more efficiently? You said something about using "enum" instead of "#define." How do I go about that?
__________________
Roland SC-55 Music Packs Website
- MediaFire mirror for Duke3D expansions
More SC-55 music packs coming soon...
Last edited by MusicallyInspired; 05-15-2008 at 04:03 PM.
MusicallyInspired is offline  
Old 05-15-2008, 10:19 PM   #14
Jiminator

Jiminator's Avatar
Re: String Problems in C
Welcome to modern launguages. You would probably be surprised to know your program only takes a few k, but it is probably linking in every library that exists. You can probably trim them through settings, but it is likely to still be woefully memory wasteful. Microsoft & such like to think that is ok, since the hardware improvements minimize the code bloatage.
__________________
big badass nasty weapons here....
Jiminator is offline  
Old 05-17-2008, 08:22 AM   #15
rg3
Re: String Problems in C
In Unix, for example, there's a system call called "exec" that replaces the current process by another one. Ideally, that's the type of system call your program should use. When you select a game, the game is launched and your program is "terminated", so only the game will be running. However, the C standard "system()" function launches another process and waits until it terminates, so you do have both programs running at the same time. You'll have to look for something similar to Unix "exec" for your platform.
rg3 is offline  
Old 05-18-2008, 10:38 AM   #16
MusicallyInspired

MusicallyInspired's Avatar
Re: String Problems in C
Ok I'm looking. Nothing so far but it must exist. Assuming I find it, how does one create command line...er...commands for a program? My idea is if i can find that function I'll run a batch file and terminate the program and then after the game is over the end of the batch file runs the menu program with a command line like "menu -3" which starts the menu program at a specific menu instead of the root menu.
__________________
Roland SC-55 Music Packs Website
- MediaFire mirror for Duke3D expansions
More SC-55 music packs coming soon...
MusicallyInspired is offline  
Old 05-18-2008, 12:42 PM   #17
theHunted
Re: String Problems in C
Quote:
Originally Posted by MusicallyInspired View Post
Assuming I find it, how does one create command line...er...commands for a program?
You simply need to declare your main function as

int main(int argc, char * argv[])
{
...
}

argc then holds the number of command line parameters given when starting your program. The values are stored as strings in argv. Usually the very first string contained in argv is the name of your executable. The first "real" parameter may follow as the second string.
__________________
M:I - New Dawn
a Max Payne 2 modification
theHunted is offline  
Old 05-18-2008, 09:10 PM   #18
Superczar

Superczar's Avatar
Re: String Problems in C
Quote:
Originally Posted by MusicallyInspired View Post
Ok, after much toil this is my current source code that works so far (here's the EXE). The only problem I've run into is that when it launches some games they won't work because the menu program is taking up too much base memory lol. Sort of defeats the purpose. Unless there's some kind of way around that. Is there anything I'm doing wrong that I can fix? Or re-code to work more efficiently? You said something about using "enum" instead of "#define." How do I go about that?
Hrm, have you tried using the spawnl() function?
__________________
'F*** Fresch!!!'.
Superczar is offline  
Old 05-19-2008, 09:43 AM   #19
MusicallyInspired

MusicallyInspired's Avatar
Re: String Problems in C
Thanks for the help, guys.

Superczar, what exactly does that do?
__________________
Roland SC-55 Music Packs Website
- MediaFire mirror for Duke3D expansions
More SC-55 music packs coming soon...
MusicallyInspired is offline  
Old 05-19-2008, 09:50 AM   #20
Superczar

Superczar's Avatar
Re: String Problems in C
http://qnxcs.unomaha.edu/help/produc.../s/spawnl.html

I don't know if you can actually use that or not though to make your game run and then come back to the menu upon exit and still be within the memory limit. According to my C/C++ book though, it sounds like that's how you would run large programs in DOS within the 640k limit. "Overlays" as it was called.
__________________
'F*** Fresch!!!'.
Superczar is offline  
Old 06-08-2008, 10:51 AM   #21
MusicallyInspired

MusicallyInspired's Avatar
Re: String Problems in C
Been a while since I've worked on this but I came back to it today and changed some things around. I don't have the source code with me at this computer but I'll post it later on. Does spawnl() work in Turbo C? I think that's the answer to my memory problem if it does. Will that work with running batch files or does it need to be an EXE file?

EDIT: Well it seems batch files aren't compatible. Anyway, I've tried both spawnl() and execl() for EXE files and neither seem to work. It just pauses for a moment and returns to the command prompt (when I do P_OVERLAY for the spawnl() mode). I gave the NULL argument and still it does nothing.

EDIT 2: Figured out what was wrong. I wasn't declaring the program itself as the first argument which apparently I was supposed to do. Anyway, everything's working more or less perfect now. Games that don't need a lot of memory are simply called using system() and it goes back to the menu again no problem. What I'm having trouble with is figuring out a way to get the games that require spawnl() or execl() to launch the menu again after they terminate. Right now they simply return to DOS. exec() and spawnl() can't execute batch files which was my whole plan for re-launching the menu. Are there any other methods of doing this or am I stuck?
__________________
Roland SC-55 Music Packs Website
- MediaFire mirror for Duke3D expansions
More SC-55 music packs coming soon...
Last edited by MusicallyInspired; 06-08-2008 at 01:31 PM.
MusicallyInspired is offline  
Old 06-08-2008, 02:06 PM   #22
Superczar

Superczar's Avatar
Re: String Problems in C
Can't you just use the P_WAIT argument instead of P_OVERLAY?
__________________
'F*** Fresch!!!'.
Last edited by Superczar; 06-08-2008 at 02:11 PM.
Superczar is offline  
Old 06-08-2008, 02:49 PM   #23
rg3
Re: String Problems in C
I'd create a BAT file that launches the menu program in a loop. This has a minor problem, which is that there's no way to realiabily detect the menu level you were at. But at least you would be back to the menu after the game finishes, and if your program is fast and easy to use, it's not a problem to type a couple of numbers followed by the Enter key to launch the next game.

Not related to this, I had something similar in place when I used DOS 10 years ago, but it was not a C program. When the computer finished booting, autoexec.bat would print a menu on screen with some options, like "1. WordPerfect; 2. Games; etc". All very pretty. And, in C:\, I had some BAT scripts called 1.bat, 2.bat, etc. So when I used "1" as a command, WordPerfect would start. If I used "2", I would CD into C:\GAMES and print a new menu. In C:\GAMES I had other different 1.bat, 2.bat files for each game in the menu. It worked pretty well. If I recall correctly, at any level, the command "M" would print the menu for that directory.
rg3 is offline  
Old 06-08-2008, 03:49 PM   #24
MusicallyInspired

MusicallyInspired's Avatar
Re: String Problems in C
Quote:
Originally Posted by Superczar View Post
Can't you just use the P_WAIT argument instead of P_OVERLAY?
That's basically the same thing as using system(). The menu program itself is taking up too much memory for some games to even start. So I need the menu program to terminate before those games run.

Quote:
Originally Posted by rg3 View Post
I'd create a BAT file that launches the menu program in a loop. This has a minor problem, which is that there's no way to realiabily detect the menu level you were at. But at least you would be back to the menu after the game finishes, and if your program is fast and easy to use, it's not a problem to type a couple of numbers followed by the Enter key to launch the next game.
Well, I've created my menu program so that if you give it an argument it will start the menu program at a specific menu. Like "menu 1" will start the menu in the adventure game menu. Does that help? How would I got about making a BAT loop? I originally had my menu as a BAT menu in itself, but I wanted to program it in C with a button selector.

Quote:
Not related to this, I had something similar in place when I used DOS 10 years ago, but it was not a C program. When the computer finished booting, autoexec.bat would print a menu on screen with some options, like "1. WordPerfect; 2. Games; etc". All very pretty. And, in C:\, I had some BAT scripts called 1.bat, 2.bat, etc. So when I used "1" as a command, WordPerfect would start. If I used "2", I would CD into C:\GAMES and print a new menu. In C:\GAMES I had other different 1.bat, 2.bat files for each game in the menu. It worked pretty well. If I recall correctly, at any level, the command "M" would print the menu for that directory.
Yeah, that's the autoexec/config.sys boot-up menu. I have that currently with just a few selections for whether or not to load EMM386, my VESA driver, or startup Windows 95.
__________________
Roland SC-55 Music Packs Website
- MediaFire mirror for Duke3D expansions
More SC-55 music packs coming soon...
MusicallyInspired is offline  
Old 06-08-2008, 06:52 PM   #25
Superczar

Superczar's Avatar
Re: String Problems in C
Ah shit hmm, there has to be a way though...

[EDIT] An extender maybe? To use EMS?
__________________
'F*** Fresch!!!'.
Last edited by Superczar; 06-08-2008 at 07:50 PM.
Superczar is offline  
Old 06-09-2008, 12:18 PM   #26
Qbix
DOSBox Advisor

Qbix's Avatar
Re: String Problems in C
why not make use of a simple batch file trick

bat1.bat
Code:
:menu
menu.exe
call game.bat
goto menu
and rewrite game.bat to start the game if you want to run it. Exit the menu and the right game will run. (maybe save in a file where you were in the menu)
Qbix is offline  
Old 06-09-2008, 07:56 PM   #27
MusicallyInspired

MusicallyInspired's Avatar
Re: String Problems in C
I made a batch file loop but set it so that if it detects the errorlevel as 1 it'll break the batch loop. Then I set the menu program so that if you press escape to quit the menu it changes errorlevel to 1. This way when I use execl for the games that need more memory it'll go back into the menu program after it quits by way of the batch loop. This works wonders except for the fact that I'm not sure how to set it so that it loops back into the menu at a specific menu list. The menu command line arguments don't seem to do much as I'd have to set an errorlevel for each menu's instance upon game launch but execl doesn't have a function for setting errorlevel and it wouldn't matter anyway as a game would set the errorlevel back to 0 upon quitting, right? I think what I might have to do is have the program create a file and I can just have the batch file check to see if a certain file exists and, if it does, act accordingly as to which menu it has to load up via command line argument the information of which is stored in the file created by the program just before launch of the game that requires execl.

So, how do you create/delete a file in C and write specific strings or characters into it?
__________________
Roland SC-55 Music Packs Website
- MediaFire mirror for Duke3D expansions
More SC-55 music packs coming soon...
Last edited by MusicallyInspired; 06-09-2008 at 08:09 PM.
MusicallyInspired is offline  
Old 06-10-2008, 07:45 PM   #28
Superczar

Superczar's Avatar
Re: String Problems in C
For writing chars/strings to file, its fairly simple actually.

http://www.cplusplus.com/reference/c...dio/fputc.html
http://www.cplusplus.com/reference/c...dio/fputs.html

fopen() using the 'w' mode should create a file for writing to (or replace a file if it already exists)... 'w+' for read and write...

http://www.cplusplus.com/reference/c...dio/fopen.html
__________________
'F*** Fresch!!!'.
Superczar is offline  
Old 06-10-2008, 09:14 PM   #29
MusicallyInspired

MusicallyInspired's Avatar
Re: String Problems in C
Thanks. Another quick question: is it possible for batch files to read the contents of a file? Probably not. Just check to see if certain files exist, right?. So I'm guessing I'll just have to create multiple files and have the batch loop check to see which file is there if any at all. And also, how do you delete a file in C?

I appreciate this help. This menu's almost done.
__________________
Roland SC-55 Music Packs Website
- MediaFire mirror for Duke3D expansions
More SC-55 music packs coming soon...
MusicallyInspired is offline  
Old 06-10-2008, 11:44 PM   #30
Superczar

Superczar's Avatar
Re: String Problems in C
remove() to delete a file...

http://www.cplusplus.com/reference/c...io/remove.html

You can have a batch file read from a text file using the FOR command, but you may want to find something with examples on the net. You can goto a prompt and type "for /?" to get a lengthy description...
__________________
'F*** Fresch!!!'.
Superczar is offline  
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 07:23 PM.

Page generated in 0.20874190 seconds (100.00% PHP - 0% MySQL) with 18 queries

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.

Website is 1987-2014 Apogee Software, Ltd.
Ideas and messages posted here become property of Apogee Software Ltd.